Click here to Skip to main content
15,886,919 members
Home / Discussions / C#
   

C#

 
QuestionUnable to Receive data from Cobas e411 Serial Port Pin
Member 1493027227-Jun-23 22:20
Member 1493027227-Jun-23 22:20 
AnswerRe: Unable to Receive data from Cobas e411 Serial Port Pin
Richard MacCutchan27-Jun-23 23:08
mveRichard MacCutchan27-Jun-23 23:08 
AnswerRe: Unable to Receive data from Cobas e411 Serial Port Pin
OriginalGriff27-Jun-23 23:38
mveOriginalGriff27-Jun-23 23:38 
AnswerRe: Unable to Receive data from Cobas e411 Serial Port Pin
Ralf Meier28-Jun-23 0:43
mveRalf Meier28-Jun-23 0:43 
AnswerRe: Unable to Receive data from Cobas e411 Serial Port Pin
jschell28-Jun-23 6:08
jschell28-Jun-23 6:08 
GeneralRe: Unable to Receive data from Cobas e411 Serial Port Pin
Member 1493027228-Jun-23 6:21
Member 1493027228-Jun-23 6:21 
AnswerRe: Unable to Receive data from Cobas e411 Serial Port Pin
Gerry Schmitz28-Jun-23 6:51
mveGerry Schmitz28-Jun-23 6:51 
QuestionGoogle PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois22-Jun-23 10:03
professionalKevin Marois22-Jun-23 10:03 
We have an an app that allows the user to import their Google contacts. We're modifying it because Google changed their auth process.

The idea is to call the Google Auth page and use an HTTPListener to listen on a port for the redirect URI. I went to the Google API Console [^]and set the Redirect URI to "https://127.0.0.1". We can't hardcode a port here.

There's a WPF sample from Google[^] here. Copy to a WPF window to see how it works.

So I set up the HTTPListener to handle the callback. The problem is that by using a random port number, how can you specify a callback URI in the control panel? Here's my code:
public async Task TryAuthorizeAsync()
{
    // Generates state and PKCE values.
    string state = RandomDataBase64url(32);
    string codeVerifier = RandomDataBase64url(32);
    string codeChallenge = Base64urlencodeNoPadding(SHA256(codeVerifier));

    // Creates an HttpListener to listen for requests
    string localHost = $"https://{IPAddress.Loopback}";
    string listenerURI = $"{localHost}:{GetRandomUnusedPort()}/"; // Includes a random port #
    var listener = new HttpListener();
    RaiseStatusChanged($"listening on {listenerURI}");
    listener.Prefixes.Add(listenerURI);

    listener.Start();

    // Create the authorization request passing <a href="https://127.0.0.1">https://127.0.0.1</a> as the redirect.
    var authorizationRequest = $"{AuthorizationEndpoint}?response_type=code&" +
                                $"scope=openid%20profile&" +
                                $"redirect_uri={Uri.EscapeDataString(localHost)}&" +
                                $"client_id={ClientID}&" +
                                $"state={state}&" +
                                $"code_challenge={codeChallenge}&" +
                                $"code_challenge_method={CODE_CHALLENEGE_METHOD}";

    // Opens request in the default browser
    Process.Start(authorizationRequest);

    bool success = false;

    // Wait for the auth authorization response.
    await Task.Run(() =>
    {
        // Begin waiting for context. Call the ListenerCallback when context is recieved
        IAsyncResult context2 = listener.BeginGetContext(new AsyncCallback(result => ListenerCallback(result, state, codeVerifier, listenerURI)), listener);

        if (HANDLE_TIMEOUT)
        {
            success = context2.AsyncWaitHandle.WaitOne(RESPONSE_TIMEOUT, true);
        }
    });

    // If here and both handling the timeout and the auth was not successfull, then the user
    // didn't do anything in the browser, so the auth process timed out. If the user did
    // anything in the browser, the ListenerCallback method handles it.
    if (HANDLE_TIMEOUT && !success)
    {
        RaiseAuthorizationTimedOut();
    }
}
When I run this, the user's Google auth page opens in the browser and when the user selects their account and authorizes, but the ListenerCallback never fires.

What I do see is the page directs to
This site can't be reached
127.0.0.1 refused to connect
The problem seems to be is that the auth needs the redirect port number. But we can't use a dedicated port number in the Google API Console, so how then can I redirect to the listener method?
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.


modified 23-Jun-23 12:30pm.

AnswerRe: Google PeopleAPI & HttpListener Redirect URI Pin
jschell23-Jun-23 10:45
jschell23-Jun-23 10:45 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois23-Jun-23 12:18
professionalKevin Marois23-Jun-23 12:18 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
Richard Andrew x6425-Jun-23 11:44
professionalRichard Andrew x6425-Jun-23 11:44 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois25-Jun-23 17:41
professionalKevin Marois25-Jun-23 17:41 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
jschell26-Jun-23 5:31
jschell26-Jun-23 5:31 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois26-Jun-23 8:20
professionalKevin Marois26-Jun-23 8:20 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois26-Jun-23 16:19
professionalKevin Marois26-Jun-23 16:19 
GeneralRe: Google PeopleAPI & HttpListener Redirect URI Pin
jschell27-Jun-23 4:48
jschell27-Jun-23 4:48 
General[UPDATE] - Google PeopleAPI & HttpListener Redirect URI Pin
Kevin Marois27-Jun-23 6:01
professionalKevin Marois27-Jun-23 6:01 
GeneralRe: [UPDATE] - Google PeopleAPI & HttpListener Redirect URI Pin
jschell28-Jun-23 5:54
jschell28-Jun-23 5:54 
QuestionThreading related naming and usage of const Pin
HobbyProggy20-Jun-23 5:04
professionalHobbyProggy20-Jun-23 5:04 
AnswerRe: Threading related naming and usage of const Pin
Richard Deeming20-Jun-23 6:05
mveRichard Deeming20-Jun-23 6:05 
GeneralRe: Threading related naming and usage of const Pin
HobbyProggy20-Jun-23 23:02
professionalHobbyProggy20-Jun-23 23:02 
GeneralRe: Threading related naming and usage of const Pin
Richard Deeming20-Jun-23 23:26
mveRichard Deeming20-Jun-23 23:26 
GeneralRe: Threading related naming and usage of const Pin
HobbyProggy21-Jun-23 2:02
professionalHobbyProggy21-Jun-23 2:02 
GeneralRe: Threading related naming and usage of const Pin
Richard Deeming21-Jun-23 2:22
mveRichard Deeming21-Jun-23 2:22 
GeneralRe: Threading related naming and usage of const Pin
HobbyProggy21-Jun-23 3:16
professionalHobbyProggy21-Jun-23 3:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.