Search code examples
androidxamarinoauthxamarin.androidgoogle-oauth

Redirect url for Google OAuth Android


Never really worked with OAuth, trying to implement it now, I want to get access token and profile data from google and facebook. Using Xamarin.Auth.

With Facebook there're no problems, I specify "http://www.facebook.com/connect/login_success.html" as redirect url and after I login it goes back to the activity I was before.

However with Google it's not as smooth - couldn't find any similar to facebook login success pages, somewhere found suggestion to use "https://www.googleapis.com/plus/v1/people/me" - added it to redirect url white list, however after sign in I would get "Redirect_url_mismatch" A native application: application nameaccording to their documentation I should use "my.package.name:" and again I added that to redirect url white list, attempted to sign in, this time after sign in screen I get to second screen where I need to confirm read permissions and after that I get very short error something like "com.my.package:/?oauthparameterX=value1...." and get redirected to permission screen again.

Here's my complete OAuth2Authenticator:

var auth = new OAuth2Authenticator(
            clientId: SocialIds.GooglePlusId,
            clientSecret: SocialIds.GooglePlusSecret,
            scope: OAuthUrl.GoogleScope,
            authorizeUrl: new Uri(OAuthUrl.GoogleAuthorize),
            redirectUrl: new Uri("https://www.googleapis.com/plus/v1/people/me"),
            accessTokenUrl: new Uri("https://accounts.google.com/o/oauth2/token"),
            getUsernameAsync: null);
            auth.AllowCancel = false;

urls:

public static string GoogleAuthorize = "https://accounts.google.com/o/oauth2/auth";
public static string GoogleScope = "https://www.googleapis.com/auth/userinfo.email";
public static string GoogleRedirect = "https://www.googleapis.com/plus/v1/people/me";
public static string GoogleUserInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token={0}";

Solution

  • In the above listed code, you are not giving the redirect uri instead you are giving the scope of google api. The purpose of redirect uri is to recieve the response from google api after authorization. The response should be a code. This response code is used for accessing the access_token,refresh_token, id_token etc. So you have to recieve this code in your project side.For this purpose , the redirect uri is used. Go to your google console, create project, add credentials, then you will be redirect to a page conatains,

    enter image description here

    You can find the authorized redirect url. Give the url , then configure your code with new redirect url . Everything will be fine after this.