/// <summary>
        /// Exchanges the user-authorized request token for an OAuth 2.0 access token.
        /// When successful, updates the OAuthParameter instance passed as parameter by setting
        /// AccessToken, RefreshToken and TokenExpiry.
        /// </summary>
        /// <param name="queryString">The query string containing the access code</param>
        /// <param name="parameters">The OAuth 2.0 parameters</param>
        public static void GetAccessToken(String queryString, OAuth2Parameters parameters)
        {
            // split results and update parameters
            SortedDictionary <string, string> responseValues = OAuthBase.GetQueryParameters(queryString);

            parameters.AccessCode = responseValues[OAuthBase.OAuth2AccessCode];
            GetAccessToken(parameters);
        }
        /// <summary>
        /// Send the request body to the OAuth 2.0 token endpoint to retrieve an access token.
        /// When successful, updates the OAuthParameter instance passed as parameter by setting
        /// AccessToken, RefreshToken and TokenExpiry.
        /// </summary>
        /// <param name="parameters">The OAuth 2.0 parameters</param>
        /// <param name="requestBody">The request body to send</param>
        public static void GetOAuth2AccessToken(OAuth2Parameters parameters, String requestBody)
        {
            Uri        requestUri = new Uri(parameters.TokenUri);
            WebRequest request    = WebRequest.Create(requestUri);

            request.Method = "POST";

            request.ContentType = "application/x-www-form-urlencoded";

            Stream       outputStream = request.GetRequestStream();
            StreamWriter w            = new StreamWriter(outputStream);

            w.Write(requestBody);
            w.Flush();
            w.Close();

            WebResponse response = request.GetResponse();
            string      result   = "";

            if (response != null)
            {
                Stream       responseStream = response.GetResponseStream();
                StreamReader reader         = new StreamReader(responseStream);
                result = reader.ReadToEnd();

                Dictionary <string, string> dict = JsonConvert.DeserializeObject <Dictionary <string, string> >(result);

                if (dict.ContainsKey(OAuth2AccessToken))
                {
                    parameters.AccessToken = dict[OAuth2AccessToken];
                }

                if (dict.ContainsKey(OAuth2RefreshToken))
                {
                    parameters.RefreshToken = dict[OAuth2RefreshToken];
                }

                if (dict.ContainsKey(OAuth2TokenType))
                {
                    parameters.TokenType = dict[OAuth2TokenType];
                }

                if (dict.ContainsKey(OAuth2ExpiresIn))
                {
                    parameters.TokenExpiry = DateTime.Now.AddSeconds(int.Parse(dict[OAuth2ExpiresIn]));
                }
            }
        }
        /// <summary>
        /// Generate the request body for refreshing an access token.
        /// </summary>
        /// <param name="parameters">The OAuth 2.0 parameters</param>
        public static String GetRefreshAccessTokenRequestBody(OAuth2Parameters parameters)
        {
            if (string.IsNullOrEmpty(parameters.RefreshToken))
            {
                throw new ArgumentNullException("refresh_token");
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0}={1}", OAuth2GrantType, "refresh_token");
            sb.AppendFormat("&{0}={1}", OAuth2ClientId, EncodingPerRFC3986(parameters.ClientId));
            sb.AppendFormat("&{0}={1}", OAuth2ClientSecret, EncodingPerRFC3986(parameters.ClientSecret));
            sb.AppendFormat("&{0}={1}", OAuth2RefreshToken, EncodingPerRFC3986(parameters.RefreshToken));

            return(sb.ToString());
        }
        /// <summary>
        /// Generate the request body for exchanging an access code for an access token.
        /// </summary>
        /// <param name="parameters">The OAuth 2.0 parameters</param>
        public static String GetExchangeAccessCodeRequestBody(OAuth2Parameters parameters)
        {
            if (string.IsNullOrEmpty(parameters.AccessCode))
            {
                throw new ArgumentNullException("access_code");
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("{0}={1}", OAuth2GrantType, "authorization_code");
            sb.AppendFormat("&{0}={1}", OAuth2ClientId, EncodingPerRFC3986(parameters.ClientId));
            sb.AppendFormat("&{0}={1}", OAuth2ClientSecret, EncodingPerRFC3986(parameters.ClientSecret));
            sb.AppendFormat("&{0}={1}", OAuth2AccessCode, EncodingPerRFC3986(parameters.AccessCode));
            sb.AppendFormat("&{0}={1}", OAuth2RedirectUri, EncodingPerRFC3986(parameters.RedirectUri));

            return(sb.ToString());
        }
        /// <summary>
        /// Generates the url which the user should visit in order to authenticate and
        /// authorize with the Service Provider.
        /// When successful, updates the OAuth2Parameters instance passed as parameter by setting
        /// the returned access code.
        /// </summary>
        /// <param name="parameters">The OAuth 2.0 parameters</param>
        /// <returns>The full authorization url the user should visit</returns>
        public static string CreateOAuth2AuthorizationUrl(OAuth2Parameters parameters)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(parameters.AuthUri);

            sb.AppendFormat("?{0}={1}", OAuthBase.OAuth2ResponseType, OAuthBase.EncodingPerRFC3986(parameters.ResponseType));
            sb.AppendFormat("&{0}={1}", OAuthBase.OAuth2ClientId, OAuthBase.EncodingPerRFC3986(parameters.ClientId));
            sb.AppendFormat("&{0}={1}", OAuthBase.OAuth2RedirectUri, OAuthBase.EncodingPerRFC3986(parameters.RedirectUri));
            sb.AppendFormat("&{0}={1}", OAuthBase.OAuthScopeKey, OAuthBase.EncodingPerRFC3986(parameters.Scope));
            sb.AppendFormat("&{0}={1}", OAuthBase.OAuth2AccessType, OAuthBase.EncodingPerRFC3986(parameters.AccessType));
            sb.AppendFormat("&{0}={1}", OAuthBase.OAuth2ApprovalPrompt, OAuthBase.EncodingPerRFC3986(parameters.ApprovalPrompt));
            if (!string.IsNullOrEmpty(parameters.State))
            {
                sb.AppendFormat("&{0}={1}", OAuthBase.OAuth2State, OAuthBase.EncodingPerRFC3986(parameters.State));
            }

            return(sb.ToString());
        }
示例#6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public GOAuth2RequestFactory(string service, string applicationName, OAuth2Parameters parameters)
     : base(service, applicationName)
 {
     Parameters = parameters;
 }
 /// <summary>
 /// Refresh the OAuth 2.0 access token.
 /// When successful, updates the OAuthParameter instance passed as parameter by setting
 /// AccessToken, RefreshToken and TokenExpiry.
 /// </summary>
 /// <param name="parameters">The OAuth 2.0 parameters</param>
 public static void RefreshAccessToken(OAuth2Parameters parameters)
 {
     OAuthBase.GetOAuth2AccessToken(parameters, OAuthBase.GetRefreshAccessTokenRequestBody(parameters));
 }
 /// <summary>
 /// Exchanges the user-authorized request token for an OAuth 2.0 access token.
 /// When successful, updates the OAuthParameter instance passed as parameter by setting
 /// AccessToken, RefreshToken and TokenExpiry.
 /// </summary>
 /// <param name="parameters">The OAuth 2.0 parameters</param>
 public static void GetAccessToken(OAuth2Parameters parameters)
 {
     OAuthBase.GetOAuth2AccessToken(parameters, OAuthBase.GetExchangeAccessCodeRequestBody(parameters));
 }
 /// <summary>
 /// a constructor for OAuth 2.0
 /// </summary>
 /// <param name="applicationName">The name of the application</param>
 /// <param name="consumerKey">the consumerKey to use</param>
 /// <param name="consumerSecret">the consumerSecret to use</param>
 /// <param name="token">The token to be used</param>
 /// <param name="tokenSecret">The tokenSecret to be used</param>
 /// <returns></returns>
 public OAuth2Authenticator(string applicationName, OAuth2Parameters parameters)
     : base(applicationName)
 {
     _parameters = parameters;
 }