GetTokenByRefreshTokenAsync() public method

If a user has authorized their account but you no longer have a valid access_token for them, then a new one can be generated by using the refreshToken.

When your application receives a refresh token, it is important to store that refresh token for future use.

If your application loses the refresh token, you will have to prompt the user for their login information again.

/// Thrown when a null reference is passed to a method that does not accept it as a /// valid argument. /// Thrown when an error is found in a response from an Imgur endpoint. Thrown when an error is found in a response from a Mashape endpoint.
public GetTokenByRefreshTokenAsync ( string refreshToken ) : Task
refreshToken string
return Task
Esempio n. 1
0
        private IOAuth2Token GetOAuth2Token()
        {
            if (_token != null)
                return _token;

            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint = new OAuth2Endpoint(authentication);
            _token = endpoint.GetTokenByRefreshTokenAsync(RefreshToken).Result;
            return _token;
        }
Esempio n. 2
0
        public async Task GetTokenByRefreshTokenAsync_SetToken_IsNotNull()
        {
            var authentication = new ImgurClient(ClientId, ClientSecret);
            var endpoint = new OAuth2Endpoint(authentication);
            var token = await endpoint.GetTokenByRefreshTokenAsync(RefreshToken);

            Assert.IsNotNull(token);
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccessToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.RefreshToken));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.AccountId));
            Assert.IsFalse(string.IsNullOrWhiteSpace(token.TokenType));
        }
Esempio n. 3
0
 public async Task GetTokenByRefreshTokenAsync_WithTokenNull_ThrowsArgumentNullException()
 {
     var client = new ImgurClient("123", "1234");
     var endpoint = new OAuth2Endpoint(client);
     await endpoint.GetTokenByRefreshTokenAsync(null);
 }
Esempio n. 4
0
        public async Task GetTokenByRefreshTokenAsync_AreEqual()
        {
            var fakeHttpMessageHandler = new FakeHttpMessageHandler();
            var fakeResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(OAuth2EndpointResponses.OAuth2TokenRefreshTokenResponse)
            };

            fakeHttpMessageHandler.AddFakeResponse(new Uri("https://api.imgur.com/oauth2/token"), fakeResponse);

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(fakeHttpMessageHandler));
            var token = await endpoint.GetTokenByRefreshTokenAsync("xhjhjhj");

            Assert.AreEqual("RefreshTokenResponse", token.AccessToken);
            Assert.AreEqual("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.AreEqual("bearer", token.TokenType);
            Assert.AreEqual(2419200, token.ExpiresIn);
            Assert.AreEqual("Bob", token.AccountUsername);
            Assert.AreEqual("45344", token.AccountId);
        }
        public async Task GetTokenByRefreshTokenAsync_WithTokenNull_ThrowsArgumentNullException()
        {
            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                    Record.ExceptionAsync(
                        async () => await endpoint.GetTokenByRefreshTokenAsync(null).ConfigureAwait(false))
                        .ConfigureAwait(false);
            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);
        }
        public async Task GetTokenByRefreshTokenAsync_WithClientSecretNull_ThrowsArgumentNullException()
        {
            var client = new ImgurClient("123");
            var endpoint = new OAuth2Endpoint(client);

            var exception =
                await
                    Record.ExceptionAsync(
                        async () => await endpoint.GetTokenByRefreshTokenAsync("ahkjhkjhc").ConfigureAwait(false))
                        .ConfigureAwait(false);
            Assert.NotNull(exception);
            Assert.IsType<ArgumentNullException>(exception);

            var argNullException = (ArgumentNullException) exception;
            Assert.Equal(argNullException.ParamName, "ClientSecret");
        }
        public async Task GetTokenByRefreshTokenAsync_Equal()
        {
            var mockUrl = "https://api.imgur.com/oauth2/token";
            var mockResponse = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(MockOAuth2EndpointResponses.GetTokenByRefreshToken)
            };

            var client = new ImgurClient("123", "1234");
            var endpoint = new OAuth2Endpoint(client, new HttpClient(new MockHttpMessageHandler(mockUrl, mockResponse)));
            var token = await endpoint.GetTokenByRefreshTokenAsync("xhjhjhj").ConfigureAwait(false);

            Assert.Equal("RefreshTokenResponse", token.AccessToken);
            Assert.Equal("2132d34234jkljj84ce0c16fjkljfsdfdc70", token.RefreshToken);
            Assert.Equal("bearer", token.TokenType);
            Assert.Equal(2419200, token.ExpiresIn);
            Assert.Equal("Bob", token.AccountUsername);
            Assert.Equal("45344", token.AccountId);
        }