Пример #1
0
        // Remember that TaskCompleted runs in a background thread.
        // We cannot just update UI elements directly..
        void oauth2_OnTaskCompleted(object sender, Chilkat.TaskCompletedEventArgs args)
        {
            fgAppendToLog("Task Completed.  Flow state = " + m_oauth2.AuthFlowState.ToString() + "\r\n");

            if (m_oauth2.AuthFlowState == 3)
            {
                // Success!
                fgSetAccessToken(m_oauth2.AccessToken);

                // Some providers, such as Facebook, do not provide a refresh token (and thus RefreshToken will be empty).
                fgSetRefreshToken(m_oauth2.RefreshToken);

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Access Granted");
                sb.AppendLine(m_oauth2.AccessTokenResponse);
                fgAppendToLog(sb.ToString());
            }
            else if (m_oauth2.AuthFlowState == 4)
            {
                // Access Denied.  The user interactive choose to deny access (in the browser) rather than click on the "Allow" button.
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Access Denied");
                sb.AppendLine(m_oauth2.AccessTokenResponse);
                fgAppendToLog(sb.ToString());
            }
            else
            {
                // Some other error happened and the OAuth2 did not complete.
                fgAppendToLog(m_oauth2.FailureInfo);
            }
        }
Пример #2
0
        // This event fires in a background thread, so be careful about touching UI buttons, textboxes, etc.
        void listenSocket_OnTaskCompleted(object sender, Chilkat.TaskCompletedEventArgs args)
        {
            if (args.Task.LastMethodSuccess)
            {
                // We'll be acting as an HTTP server.
                // We'll read the incoming HTTP request and send a response.
                Chilkat.Socket httpServerSock = new Chilkat.Socket();

                // Let's inject this Chilkat.Socket object with the results of the task.
                httpServerSock.LoadTaskResult(args.Task);

                // Read the incoming start line..
                string startLine = httpServerSock.ReceiveUntilMatch("\r\n");

                // Read the HTTP request.  We'll read to the first double CRLF, which is to the end of the
                // request header.  This should be all that is coming because the request should be a GET request (i.e. no request body).
                string requestHeader = httpServerSock.ReceiveUntilMatch("\r\n\r\n");

                // The HTTP request's startLine contains the information we need..
                // It looks like this:
                //  GET /?state=ARudjbBgI8FxgNGqEdUsv1TfYL4rAkOdDObQUT-dV8g&code=4/ovg2Tct4_Ct-BUSPnBRKyXJqsO4nGj9FNxqexxD0KK8&authuser=0&session_state=93ef25f6921934eed290ca484acb58653585ee71..bed8&prompt=consent HTTP/1.1

                // Parse the startLine by getting rid of the "GET" and "HTTP/1.1", and making it a URL that we can load into a Chilkat.HttpRequest object.
                string tempUrl = "http://www.anything.com" + startLine.Replace("GET ", "").Replace(" HTTP/1.1", "").Trim();
                Chilkat.HttpRequest tempReq = new Chilkat.HttpRequest();
                tempReq.SetFromUrl(tempUrl);

                string state         = tempReq.GetParam("state");
                string code          = tempReq.GetParam("code");
                string session_state = tempReq.GetParam("session_state");

                // Now send a response..
                string responseString = string.Format("<html><body><ul><li>state: " + state + "<li>code: " + code + "<li>session_state: " + session_state + "</ul></body></html>");
                httpServerSock.SendString(responseString);

                httpServerSock.Close(10);

                fgAppendToErrorLog(startLine + requestHeader + "\r\n----\r\n");

                // Now exchange the code for an access token and a refresh token.
                // (The args.Task.UserData contains the JSON we initially stashed in the Task's UserData property.)
                googleExchangeCodeForToken(code, args.Task.UserData);
            }
            else
            {
                // Failed...
                fgAppendToErrorLog(args.Task.ResultErrorText);
            }
        }
Пример #3
0
        // Remember, this callback happens in a background thread...
        void m_clientSock_OnTaskCompleted(object sender, Chilkat.TaskCompletedEventArgs args)
        {
            if (args.Task.UserData.Equals("connect"))
            {
                if (args.Task.GetResultBool() == false)
                {
                    fgAppendToTextBox1("Connect failed!\r\n");
                    fgAppendToTextBox1(args.Task.ResultErrorText);
                    return;
                }

                // This was our ConnectAsync call that completed...
                fgAppendToTextBox1("Connect completed.\r\n");
                sendHttpGet();
            }
            else if (args.Task.UserData.Equals("sendHttpGet"))
            {
                if (args.Task.GetResultBool() == false)
                {
                    fgAppendToTextBox1("Send GET failed!\r\n");
                    fgAppendToTextBox1(args.Task.ResultErrorText);
                    return;
                }

                fgAppendToTextBox1("GET request sent.\r\n");
                readHttpResponseHeader();
            }
            else if (args.Task.UserData.Equals("readResponseHeader"))
            {
                if (args.Task.TaskSuccess == false)
                {
                    fgAppendToTextBox1("Read response header failed!\r\n");
                    fgAppendToTextBox1(args.Task.ResultErrorText);
                    return;
                }

                fgAppendToTextBox1("Received response header.\r\n");
                string receivedString = args.Task.GetResultString();
                fgAppendToTextBox1(receivedString);

                // The receivedString contains the HTTP start line followed by the MIME response header.
                // (for this example, discard the start line)
                int    endOfFirstLine = receivedString.IndexOf("\r\n");
                string responseHeader = receivedString.Substring(endOfFirstLine + 2);

                // Parse the MIME to find out the size of the HTTP response body.
                Chilkat.Mime mime = new Chilkat.Mime();
                mime.LoadMime(responseHeader);
                string strContentLength = mime.GetHeaderField("Content-Length");
                if (strContentLength == null)
                {
                    fgAppendToTextBox1("No Content-Length header in the response!\r\n");
                    return;
                }
                int contentLength = Convert.ToInt32(strContentLength);

                readHttpResponseBody(contentLength);
            }
            else if (args.Task.UserData.Equals("readResponseBody"))
            {
                if (args.Task.TaskSuccess == false)
                {
                    fgAppendToTextBox1("Read response body failed!\r\n");
                    fgAppendToTextBox1(args.Task.ResultErrorText);
                    return;
                }

                fgAppendToTextBox1("Received response body.\r\n");
                byte[] responseBody = args.Task.GetResultBytes();
                // For this example, assume utf-8.
                string responseBodyStr = System.Text.Encoding.UTF8.GetString(responseBody);
                fgAppendToTextBox1(responseBodyStr);
                fgAppendToTextBox1("\r\nHTTP GET Completed Successfully\r\n");
            }
        }