Android Google+无法获得授权码

我试图根据这篇文章获得Google+授权码:服务器端访问您的应用程序。 但是,每当我运行我的应用程序并尝试获取代码时,我都会在LogCat中获得以下内容:

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

我浏览了很多东西,从我可以告诉的是,我已经在下面的代码中正确安装了所有东西。 任何想法,为什么它没有得到一个身份验证令牌?

仅供参考,我在API控制台中有一个项目设置,该项目同时为oauth配置了应用程序和Web应用程序,并且我正在使用作用域字符串的服务器(而不是应用程序)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;
}

我不知道你是否修改了这一行来发布这个问题,但看着你发布的代码,这一行是错误的:

String scopes = "oauth2:server:client_id:<My server client ID>:scopesString";

它应该是:

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

尝试这个,

在你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();
                }

        }
链接地址: http://www.djcxy.com/p/72955.html

上一篇: Android Google+ unable to get auth code

下一篇: set ActionBarDrawerToggle at right corner