/// <summary> /// </summary> /// <returns></returns> /// <remarks> /// 1. The method is called on every Http operation used with this Authentication (from GetAuthorizationHeaderValue) /// </remarks> private Oauth2Token RefreshToken() { string RequestUri = "oauth//refresh_token"; Dictionary <string, string> dicQueryStringParameters = new Dictionary <string, string>(); List <KeyValuePair <string, string> > postBody = new List <KeyValuePair <string, string> >(); postBody.Add(new KeyValuePair <string, string>("client_id", this.AppConsumerKey)); postBody.Add(new KeyValuePair <string, string>("client_secret", this.AppConsumerSecret)); postBody.Add(new KeyValuePair <string, string>("grant_type", "refresh_token")); postBody.Add(new KeyValuePair <string, string>("refresh_token", this.Oauth2Token.refresh_token)); string contentType = "application/x-www-form-urlencoded"; string accept = "application/json"; IAuthentication Authentication = null; //no special headers PepperiHttpClient PepperiHttpClient = new PepperiHttpClient(Authentication, Logger); PepperiHttpClientResponse PepperiHttpClientResponse = PepperiHttpClient.PostFormUrlEncodedContent( this.OauthBaseUri, RequestUri, dicQueryStringParameters, postBody, contentType, accept); PepperiHttpClient.HandleError(PepperiHttpClientResponse); Oauth2Token result = PepperiJsonSerializer.DeserializeOne <Oauth2Token>(PepperiHttpClientResponse.Body); //Api returns single object return(result); }
/// <summary> /// /// </summary> /// <param name="authorization_code">The authorization code we get from service on callback, indicating that user has given valid credentinals.</param> /// <param name="redirect_uri"></param> /// <param name="scope"></param> /// <param name="Oauth2TokenRepository"></param> /// <returns></returns> public Oauth2Token GetAccessTokenByAuthorizationCode(string authorization_code, string redirect_uri, string scope, IOauth2TokenRepository Oauth2TokenRepository) { string RequestUri = "oauth//access_token"; Dictionary <string, string> dicQueryStringParameters = new Dictionary <string, string>(); List <KeyValuePair <string, string> > postBody = new List <KeyValuePair <string, string> >(); postBody.Add(new KeyValuePair <string, string>("client_id", this.AppConsumerKey)); postBody.Add(new KeyValuePair <string, string>("client_secret", this.AppConsumerSecret)); postBody.Add(new KeyValuePair <string, string>("scope", scope)); postBody.Add(new KeyValuePair <string, string>("grant_type", "authorization_code")); postBody.Add(new KeyValuePair <string, string>("code", authorization_code)); postBody.Add(new KeyValuePair <string, string>("redirect_uri", redirect_uri)); string contentType = "application/x-www-form-urlencoded"; string accept = "application/json"; IAuthentication Authentication = null; //no special headers PepperiHttpClient PepperiHttpClient = new PepperiHttpClient(Authentication, Logger); /// PepperiHttpClientResponse PepperiHttpClientResponse = PepperiHttpClient.PostFormUrlEncodedContent( this.OauthBaseUri, RequestUri, dicQueryStringParameters, postBody, contentType, accept); PepperiHttpClient.HandleError(PepperiHttpClientResponse); Oauth2Token result = PepperiJsonSerializer.DeserializeOne <Oauth2Token>(PepperiHttpClientResponse.Body); //Api returns single object //save result if (Oauth2TokenRepository != null) { Oauth2TokenRepository.SaveToken(result); } return(result); }
} //used to allow the sdk consumer to store the token in persistent memory (after renew, for example) #endregion #region Constructor /// <summary> /// creates fully initialized instance that can be used for consuming the API (and refreshing access token automatically) /// </summary> /// <param name="OauthBaseUri"></param> /// <param name="AppConsumerKey"></param> /// <param name="AppConsumerSecret"></param> /// <param name="Oauth2Token"></param> /// <param name="Oauth2TokenRepository"></param> public PublicAuthentication(ILogger Logger, string OauthBaseUri, string AppConsumerKey, string AppConsumerSecret, Oauth2Token Oauth2Token, IOauth2TokenRepository Oauth2TokenRepository) { this.Logger = Logger; this.OauthBaseUri = OauthBaseUri; this.AppConsumerKey = AppConsumerKey; this.AppConsumerSecret = AppConsumerSecret; this.Oauth2Token = Oauth2Token; this.Oauth2TokenRepository = Oauth2TokenRepository; }