简体   繁体   中英

Google Cloud Endpoints OAuth Authentication

I am developing an Android add with a back-end running on Google App Engine and recently activate OAuth2 authentication. My problem is that when I have a fresh install and the user has not previously signed in with his google account I display the sign-in dialog and afterwards he is able to login by pressing login with Facebook button. After receiving the data from Facebook I make an authorized call to my back-end, the problem is that this doesn't execute. If I restart the app and the user already has signed in using his google account then everything works normally. PS: In both cases I have the selected account name set in the credentials.

Any ideas why the request isn't sent in this case?

Code for creating the credentials:

    settings = getSharedPreferences("whostrPrefs", MODE_PRIVATE);
    credential = GoogleAccountCredential.usingAudience(this, ID);

    setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

    if(credential.getSelectedAccountName() != null)
    {
        //all ok
        Log.d("AccountName", "Everything is ok!");
    }
    else
    {
        Log.d("AccountName", "User needs to loggin!");
        chooseAccount();
    }

The setSelectedAccount method:

private void setSelectedAccountName(String accountName)
{
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(PREF_ACCOUNT_NAME, accountName);
    editor.commit();

    credential.setSelectedAccountName(accountName);
    this.accountName = accountName;
}

The chooseAccount method:

 void chooseAccount()
{
    startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}

And the snippet from the onActivityResult method:

            case REQUEST_ACCOUNT_PICKER:
            if(data != null && data.getExtras() != null)
            {
                String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
                if(accountName != null)
                {
                    setSelectedAccountName(accountName);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREF_ACCOUNT_NAME, accountName);
                    editor.commit();
                    Log.d("AccountName", "User is authorized!");
                    //user is authorized
                }
            }
            break;

And the actual call:

          Userendpoint.Builder builder = new Userendpoint.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential);
            Userendpoint service = builder.build();

           try
        {
        response = service.loginuser(params[0], params[1], params[2], params[3],              longitude,latitude).execute();
        }

Any ideas about what might be the problem? Am I doing anything wrong? The code gets to the place I make the call ... but doesn't pass it.

LE: The call to the endpoint is made using Google Login credentials (as shown in the tutorial on the Google App Engine website), but the call isn't executed at all for some reason. Calling without the credentials throws the correct unauthorized exception.

The in-built Auth of cloud Endpoints only works with Google login (or OpenID, depending on what Authentication Type you selected while creating the project).

For authentication with any other provider, you need to write custom code. You can do this by passing the facebook auth information in a API parameter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM