ImgurNet.Authentication.OAuth2Authentication.AuthorizeWithCode C# (CSharp) Метод

AuthorizeWithCode() публичный Метод

Gets the token set from the code in the query string of the callback url. Throws a if the code is not valid.
public AuthorizeWithCode ( string code ) : System.Threading.Tasks.Task
code string The code from the callback Url.
Результат System.Threading.Tasks.Task
		public async Task AuthorizeWithCode(string code)
		{
			var keyPairs = new List<KeyValuePair<string, string>>
			{
				new KeyValuePair<string, string>("client_id", ClientId),
				new KeyValuePair<string, string>("client_secret", ClientSecret),
				new KeyValuePair<string, string>("grant_type", "authorization_code"),
				new KeyValuePair<string, string>("code", code)
			};
			var multi = new FormUrlEncodedContent(keyPairs.ToArray());

			var tokens = await Request.SubmitGenericRequestAsync<OAuthTokens>(Request.HttpMethod.Post, TokenEndpoint, content: multi);

			AccessToken = tokens.AccessToken;
			RefreshToken = tokens.RefreshToken;
			ExpiresAt = DateTime.UtcNow.AddSeconds(tokens.ExpiresIn);
			AuthorizedUsername = tokens.AuthorizedUsername;
		}

Usage Example

		public async Task TestCodeAuth()
		{
			var settings = VariousFunctions.LoadTestSettings();

			// Create a new OAuth2 Authentication
			var oAuth2Authentication = new OAuth2Authentication(settings.ClientId, settings.ClientSecret, false);
			var authorizationUrl = oAuth2Authentication.CreateAuthorizationUrl(OAuth2Type.Code, "dicks");
			var code = "1234";
			try
			{
				await oAuth2Authentication.AuthorizeWithCode(code);
			}
			catch (ImgurResponseFailedException exception)
			{
				Assert.AreEqual(exception.ImgurResponse.Data.ErrorDescription, "Refresh token doesn't exist or is invalid for the client");
			}
		}