I am developping an UWP application that uses the Imgur.API imgur api wrapper.
I open the oauth2 authorization url in the user's web browser using
var success = await Windows.System.Launcher.LaunchUriAsync(imgur.getAuthorizationUrl());
Where
imgur.getAuthorizationUrl()
is the result of
var authorizationUrl = endpoint.GetAuthorizationUrl(Imgur.API.Enums.OAuth2ResponseType.Token);
I use OAuth2ResponseType.Token because Imgur's documentation says
Only token should be used, as the other methods have been deprecated.
How to know what redirect url to use and how to access the token data in a uwp application ?
To get the redirect URL, you will need to register your application in the imgur developer portal.
Once registered, you will get the client_id
, secret_key
and redirect_url
that you need to perform the OAuth2 flow.
To authenticate the user in you UWP application, you should use the WebAuthenticationBroker which will handle all the OAuth2 flow for you.
Here is the sample code extract from the class documentation:
String FacebookURL = "https://www.facebook.com/dialog/oauth?client_id=" + FacebookClientID.Text + "&redirect_uri=" + Uri.EscapeUriString(FacebookCallbackUrl.Text) + "&scope=read_stream&display=popup&response_type=token";
System.Uri StartUri = new Uri(FacebookURL);
System.Uri EndUri = new Uri(FacebookCallbackUrl.Text);
WebAuthenticationResult WebAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync(
WebAuthenticationOptions.None,
StartUri,
EndUri);
if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
OutputToken(WebAuthenticationResult.ResponseData.ToString());
}
else if (WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.ErrorHttp)
{
OutputToken("HTTP Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseErrorDetail.ToString());
}
else
{
OutputToken("Error returned by AuthenticateAsync() : " + WebAuthenticationResult.ResponseStatus.ToString());
}
You can get a full sample here: WebAuthenticationBroker sample