Beispiel #1
0
        internal static async Task <OAuth2Credentials> UpdateCredentialsAsync(OAuth2Credentials credentials, OAuth2App app, OAuth2EndPoint endPoint)
        {
            if (credentials?.AccessToken != null && DateTime.Now < credentials.Expires)
            {
                return(credentials);
            }

            var client = new RestClient(endPoint.BaseUri.TrimEnd('/') + '/');

            var tokenRequest = new RestRequest(endPoint.QueryTokenResource, Method.POST, DataFormat.FormUrlEncoded);

            tokenRequest.AddParameter("grant_type", "refresh_token");
            tokenRequest.AddParameter("client_id", app.ClientId);
            tokenRequest.AddParameter("client_secret", app.ClientSecret);
            tokenRequest.AddParameter("refresh_token", credentials.RefreshToken);

            var(response, _) = await client.SendAsync(tokenRequest).ConfigureAwait(false);

            var result = await response.DeserializeContentAsync <_QueryTokenResult>().ConfigureAwait(false);

            credentials = new OAuth2Credentials()
            {
                AccessToken  = result.access_token,
                RefreshToken = result.refresh_token,
                Expires      = DateTime.Now + TimeSpan.FromSeconds(result.expires_in),
            };
            SaveCredentials(credentials, app);

            return(credentials);
        }
Beispiel #2
0
 private static void SaveCredentials(OAuth2Credentials credentials, OAuth2App app)
 {
     using (var writer = new StreamWriter(app.CredentialsCachePath))
     {
         var serializer = new JsonSerializer();
         serializer.Serialize(writer, credentials);
     }
 }