安卓 Google+ 无法获取授权码

7

我正在尝试根据这篇文章:为您的应用程序启用服务器端访问获取Google+认证代码。然而,每当我运行我的应用程序并尝试获取代码时,LogCat 中会显示以下内容:

07-23 23:42:31.760: E/getAccessToken()(11114): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown

我浏览了很多内容,从下面的代码中可以看出我已经正确设置了一切。有什么想法为什么它没有获得授权令牌?
顺便说一下,我在API控制台中设置了一个项目,将应用程序和Web应用程序都配置为OAuth,并且我正在使用服务器(而不是应用程序)ID作为范围字符串。我还遵循了上面链接中的示例,并且应用程序运行良好,允许您登录/注销,但我只是无法获取授权代码。
private String getAccessToken() {
    String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
    String scope = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopesString;
    Bundle appActivities = new Bundle();
    appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
            "http://schemas.google.com/AddActivity");
    String code = null;
    try {
         code = GoogleAuthUtil.getToken(
                    this,                             // Context context
                    mPlusClient.getAccountName(),     // String accountName
                    scopes,                           // String scope
                    appActivities                     // Bundle bundle
                );
    } catch (IOException transientEx) {
      // network or server error, the call is expected to succeed if you try again later.
      // Don't attempt to call again immediately - the request is likely to
      // fail, you'll hit quotas or back-off.
        Log.e("getAccessToken()", "[ERROR] IOException: " + transientEx);
        return null;
    } catch (UserRecoverableAuthException e) {
           // Recover
        Log.e("getAccessToken()", "[ERROR] UserRecoverableAuthException: " + e);
        code = null;
    } catch (GoogleAuthException authEx) {
      // Failure. The call is not expected to ever succeed so it should not be
      // retried.
        Log.e("getAccessToken()", "[ERROR] GoogleAuthException: " + authEx);
      return null;
    } catch (Exception e) {
        Log.e("getAccessToken()", "[ERROR] Exception: " + e);
      throw new RuntimeException(e);
    }
    return code;
}
2个回答

3

尝试这个方法:

在你的 onCreate() 方法中编写以下内容:

new GetTokenAsync.execute();

然后编写AsyncTask以获取访问令牌,如下:

public class GetTokenAsync extends AsyncTask<String, String, String>
        {

            @Override
            protected void onPreExecute()
                {

                    super.onPreExecute();
                    plusUtilities.ShowProgressDialog("Getting Token...");

                }

            @Override
            protected String doInBackground(String... params)
                {
                    String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE;
                    try
                        {
                            // We can retrieve the token to check via
                            // tokeninfo or to pass to a service-side
                            // application.
                            String token = GoogleAuthUtil.getToken(getParent(), mPlusClient.getAccountName(), scope);
                            Log.i("OAUTH Token:", token);
                            Log.i("Scope:", "" + scope);
                            Log.i("GOOGLE_ACCOUNT_TYPE", "" + GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);

                            Person currentperson = mPlusClient.getCurrentPerson();
                               Log.i("person", currentperson.toString());

                        }
                    catch (UserRecoverableAuthException e)
                        {
                            Log.e("UserRecoverableAuthException", "UserRecoverableAuthException");
                            e.printStackTrace();
                        }
                    catch (IOException e)
                        {
                            Log.e("IOException", "IOException");
                            e.printStackTrace();
                        }
                    catch (GoogleAuthException e)
                        {
                            Log.e("GoogleAuthException", "GoogleAuthException");
                            e.printStackTrace();
                        }
                    catch (Exception e)
                        {
                            Log.e("Exception", "Exception");
                            e.printStackTrace();
                        }
                    return null;
                }

            @Override
            protected void onPostExecute(String result)
                {

                    super.onPostExecute(result);
                    plusUtilities.DissmissProgressDialog();
                }

        }

谢谢,但这个答案有几个问题不适用于我:1.从onCreate()调用将无法工作,因为用户需要进行身份验证才能使用mPlusClient.<whatever>。2.在我提供的链接中,这几乎是他们列出的“在线”令牌的解决方案。我正在尝试获取一个可以在我的服务器上验证的“离线”令牌。我有一个类似于您上面列出的变体,它很好用,我得到了一个令牌,但它不是我需要的令牌类型(与在线相反)。 - Jason
你能帮我解决这个问题吗?https://dev59.com/b3vaa4cB1Zd3GeqPJOhd - haythem souissi
为了代码行数(LOC)点赞: String scope = "oauth2:" + Scopes.PLUS_LOGIN + " " + Scopes.PLUS_PROFILE; - Mingsheng

1
我不知道你是否修改了提问时的代码,但是根据你所发布的代码,这一行是错误的:
String scopes = "oauth2:server:client_id:<My server client ID>:scopesString";

应该是:

它应该是:

String scopes = "oauth2:server:client_id:" + SERVER_CLIENT_ID + ":api_scope:" + scopeString;

抱歉,我的错。看起来我在发布时删除了那部分内容,我的代码中有“:api_scope:”。我已经更新了帖子以显示它。 - Jason
啊...我错过了将scopeString分离的步骤。我做了那个更改,现在我得到了一个"UserRecoverableAuthException: NeedPermission"错误。我试着处理它,但现在基本上我有了这个问题。我会标记这个作为正确的,因为它解决了我一开始遇到的问题...愚蠢的深夜失误 ;-) - Jason
自从什么时候开始,GoogleAuthUtil.getToken()方法会针对未在设备上注册的电子邮件抛出带有消息“BadUsername”的GoogleAuthException,而不是以前的带有消息“Non existing account 'email_address'”的IllegalArgumentException异常? http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html#getToken(android.content.Context, java.lang.String, java.lang.String) - Etienne Lawlor
@Jason,你从这个解决方案中获取到令牌了吗? - Mr.G
@Mr.G 好久不见了,但是看着我的当前代码(它可以工作并获取令牌),我没有使用 appActivities,并且我以稍微不同的方式生成了范围字符串。 - Jason
@Jason,我找到了一种检索令牌的方法,谢谢顺便提醒。 - Mr.G

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接