コード例 #1
0
        /// <summary>
        /// Make a request to get a valid access Token from the refresh token
        /// </summary>
        /// <returns>a valid access token from the refresh code request</returns>
        public GoogleOAuth2AccessToken GetAuthTokenFromReqeustToken()
        {
            byte[] contentAsBytes = null;
            int contentLength = 0;

            //Submit using TCP Protocol
            contentAsBytes = Encoding.ASCII.GetBytes(ContentBody);
            contentLength = contentAsBytes.Length;

            string header = BuildHeader(true, contentLength);
            byte[] headerAsBytes = Encoding.ASCII.GetBytes(header);

            TcpClient client = new TcpClient(Host, 443);
            Stream netStream = client.GetStream();
            SslStream sslStream = new SslStream(netStream);
            sslStream.AuthenticateAsClient(Host);

            sslStream.Write(headerAsBytes);
            sslStream.Write(contentAsBytes);
            GoogleOAuth2AccessToken token = new GoogleOAuth2AccessToken();

            StreamReader reader = new StreamReader(sslStream);

            int left;
            var quote = Convert.ToString(Convert.ToChar(34));
            while (reader.Peek() > 0)
            {
                string line = reader.ReadLine();
                if (line == null) break;

                left = line.IndexOf(": ") + 1;

                if (left > 0)
                {
                    var result = line.Substring(left).Replace(quote, "").Replace(",", "").Replace(":", "").Trim();

                    if (line.ToLower().Contains("access_token"))
                    {
                        token.access_token = result;
                    }
                    else if (line.ToLower().Contains("expires_in"))
                    {
                        token.expires_in = result;
                    }
                    else if (line.ToLower().Contains("token_type"))
                    {
                        token.token_type = result;
                    }
                    else if (line.ToLower().Contains("id_token"))
                    {
                        token.id_token = result;
                    }
                }
            }
            sslStream.Close();
            return token;
        }
コード例 #2
0
        //public GoogleOAuth2AccessToken ExchangeCodeForAccessToken(string code, string clientid, string clientsecret, string redirecturi)
        //{
        //    GoogleOAuth2AccessToken token = new GoogleOAuth2AccessToken();
        //    string encCode = Google.GData.Client.HttpUtility.UrlEncode(code);
        //    string encClientID = Google.GData.Client.HttpUtility.UrlEncode(clientid);
        //    string encClientSecret = Google.GData.Client.HttpUtility.UrlEncode(clientsecret);
        //    string encURL = Google.GData.Client.HttpUtility.UrlEncode(redirecturi);
        //    string encData = GetDataStringWithScope(encCode, encClientID, encClientSecret, encURL, string.Empty);
        //    try
        //    {
        //        Requestor.TCPRequestor tcpr = new Requestor.TCPRequestor(Requestor.Operation.POST
        //                                                                    , Requestor.HTTPFormat.FORMAT_1_1
        //                                                                    , Requestor.ContentType.X_WWW_FORM_URLENCODED
        //                                                                    , GoogleUtilityConstants.GOOGLE_ACCOUNTS_HOST
        //                                                                    , GoogleUtilityConstants.GOOGLE_AUTH_TOKEN_URI
        //                                                                    , encData
        //                                                                    , null);
        //        SslStream sslStream = (SslStream)tcpr.SubmitRequest(true);
        //        StreamReader reader = new StreamReader(sslStream);
        //        int left;
        //        var quote = Convert.ToString(Convert.ToChar(34));
        //        while (reader.Peek() > 0)
        //        {
        //            string line = reader.ReadLine();
        //            if (line == null) break;
        //            left = line.IndexOf(": ") + 1;
        //            if (left > 0)
        //            {
        //                var result = line.Substring(left).Replace(quote, "").Replace(",", "").Replace(":", "").Trim();
        //                if (line.ToLower().Contains("access_token"))
        //                {
        //                    token.access_token = result;
        //                }
        //                else if (line.ToLower().Contains("refresh_token"))
        //                {
        //                    token.refresh_token = result;
        //                }
        //                else if (line.ToLower().Contains("expires_in"))
        //                {
        //                    token.expires_in = result;
        //                }
        //                else if (line.ToLower().Contains("token_type"))
        //                {
        //                    token.token_type = result;
        //                }
        //                else if (line.ToLower().Contains("id_token"))
        //                {
        //                    token.id_token = result;
        //                }
        //            }
        //        }
        //        //return the token
        //        return token;
        //    }
        //    catch (System.Exception ex)
        //    {
        //        ReportError(ex, "ExchangeCodeForToken");
        //    }
        //    return null;
        //}
        /// <summary>
        /// Get the authorization Token from a refresh token
        /// </summary>
        /// <param name="refreshToken">The code to use to get a refresh token</param>
        /// <param name="clientid">The application client ID</param>
        /// <param name="clientsecret">The secret code for the application</param>
        /// <returns>A valid access token generated by the refresh token; expires in 60 min</returns>
        public GoogleOAuth2AccessToken GetAuthorizationTokenFromRefreshToken(string refreshToken, string clientid, string clientsecret)
        {
            GoogleOAuth2AccessToken token = new GoogleOAuth2AccessToken();
            string encData = string.Format(GetRefreshTokenDataString(
                                            Google.GData.Client.HttpUtility.UrlEncode(clientid)
                                            , Google.GData.Client.HttpUtility.UrlEncode(clientsecret)
                                            , Google.GData.Client.HttpUtility.UrlEncode(refreshToken)));

            try
            {

                GoogleTCPRequestor gr = new GoogleTCPRequestor(Requestor.Operation.POST
                                                                , Requestor.HTTPFormat.FORMAT_1_1
                                                                , Requestor.ContentType.X_WWW_FORM_URLENCODED
                                                                , GoogleUtilityConstants.GOOGLE_ACCOUNTS_HOST
                                                                , GoogleUtilityConstants.GOOGLE_AUTH_TOKEN_URI
                                                                , encData
                                                                , null
                                                                , false);
                token = gr.GetAuthTokenFromReqeustToken();
                return token;
            }
            catch (System.Exception ex)
            {
                ReportError(ex, "GetAuthorizationTokenFromRefreshToken");
            }
            return null;
        }