简体   繁体   English

Google Cloud Endpoints OAuth身份验证

[英]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. 我正在开发一个具有在Google App Engine上运行的后端的Android add,并且最近激活了OAuth2身份验证。 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. 我的问题是,当我进行全新安装且用户之前未使用其Google帐户登录时,我会显示登录对话框,然后他可以通过使用Facebook按钮登录来登录。 After receiving the data from Facebook I make an authorized call to my back-end, the problem is that this doesn't execute. 从Facebook收到数据后,我向后端进行了授权呼叫,问题是该操作无法执行。 If I restart the app and the user already has signed in using his google account then everything works normally. 如果我重新启动应用程序,并且用户已经使用他的Google帐户登录,则一切正常。 PS: In both cases I have the selected account name set in the credentials. PS:在两种情况下,我都在凭据中设置了所选的帐户名。

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: setSelectedAccount方法:

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: ChooseAccount方法:

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

And the snippet from the onActivityResult method: 还有onActivityResult方法的代码段:

            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. LE:对端点的调用是使用Google登录凭据(如Google App Engine网站上的教程中所示)进行的,但是由于某种原因根本没有执行该调用。 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). 内置的Cloud Endpoints Auth仅适用于Google登录(或OpenID,具体取决于您在创建项目时选择的身份验证类型)。

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. 您可以通过在API参数中传递facebook身份验证信息来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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