【问题标题】:Google coordinate authentication谷歌坐标认证
【发布时间】:2014-02-22 02:14:19
【问题描述】:

当我尝试连接到 Google 坐标时,我总是收到异常 GoogleAuthException

我有 Google 地图协调中心许可证。 我确实使用我的包应用程序名称和我的 SHA1 在谷歌控制台中创建了我的客户端 ID。

我在清单文件中添加了权限:

<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.NETWORK"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.INTERNET"/>

我使用这个代码:

final String SCOPE = "oauth2:https://www.googleapis.com/auth/coordinate";
try {
    mGoogleCoordinatetoken = GoogleAuthUtil.getToken(activity, email, SCOPE);
    Log.e(getClass().getSimpleName(), "token ="+mGoogleCoordinatetoken);
} catch (GooglePlayServicesAvailabilityException playEx) {
    Log.e(getClass().getSimpleName(), "GooglePlayServicesAvailabilityException "+playEx.getMessage());
} catch (UserRecoverableAuthException userAuthEx) {
    // Start the user recoverable action using the intent returned by
    // getIntent()
    Log.e(getClass().getSimpleName(), "UserRecoverableAuthException "+userAuthEx.getMessage());
} 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(getClass().getSimpleName(), "IOException "+transientEx.getMessage());
} catch (GoogleAuthException authEx) {
    // Failure. The call is not expected to ever succeed so it should not be
    // retried.
    Log.e(getClass().getSimpleName(), "GoogleAuthException "+authEx.getMessage());
}

知道如何解决这个异常吗?

例外:

01-30 22:24:53.968: E/getAccessToken()(24800): [ERROR] GoogleAuthException: com.google.android.gms.auth.GoogleAuthException: Unknown
01-30 22:24:53.998: E/AccessTokenTask(24800): mGoogleCoordinatetoken =null

【问题讨论】:

  • 如果您只需要令牌来进一步验证您的服务器后端,那么您可以使用“String SCOPE = “audience:server:client_id:”+Constants.WED_CLIENT_ID;”作为你的范围
  • 如果你要使用范围“oauth2:googleapis.com/auth/coordinate”那么你必须走与 Madhur Ahuja 建议的相同的路线..Bacuase 这个范围只是不给你令牌..它首先需要用户授权..只有这个范围“audience:server:client_id:”直接给你称为跨客户端身份的令牌
  • 我想获取工作列表,例如:developers.google.com/coordinate/v1/jobs/list,我不需要令牌吗?
  • 可以粘贴异常吗?
  • 它清楚地表明这需要授权..你需要在你的其他范围内尝试“googleapis.com/auth/coordinate”..试试这个stackoverflow.com/questions/17827256/…

标签: android google-maps oauth-2.0


【解决方案1】:

问题是为什么你需要得到一个令牌。正如您在我的问题中评论的那样,您应该可以使用 GoogleAccountCredential 对象。获得凭据对象后,您可以调用 Google API

  credential = GoogleAccountCredential.usingOAuth2(this, scopes);
                if (TextUtils.isEmpty(appPreferences.getUserName()))
                {
                        try
                        {

                                startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
                        }
                        catch (ActivityNotFoundException e)
                        {

                                Toast.makeText(this, getString(R.string.gps_missing), Toast.LENGTH_LONG).show();

                                return;
                        }

                }

【讨论】:

【解决方案2】:
    String WEB_APPLICATION_CLIENT_ID = "656631023202-9jsg9faqe87n1uo7f5g6iupti1jl2nps.apps.googleusercontent.com";
    String scopes = String.format("audience:server:client_id:" + WEB_APPLICATION_CLIENT_ID );

    Log.e(getClass().getSimpleName(), "email ="+email);

    String code = null;
    try {
         code = GoogleAuthUtil.getToken(
                 LoginActivity.this,                             // Context context
                 email,     // String accountName
                    scopes
                );
         mGoogleCoordinatetoken = code;
    } 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);
    } 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);
    } catch (Exception e) {
        Log.e("getAccessToken()", "[ERROR] Exception: " + e);
      throw new RuntimeException(e);
    }

【讨论】:

  • 我使用的是客户端 ID,而不是 WEB_APPLICATION_CLIENT_ID。
【解决方案3】:

我遇到了完全相同的问题,并通过将范围修改为:

"oauth2:https://www.googleapis.com/auth/[API YOU WANT]"

(开头很重要!)

希望对某人有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 2023-03-17
    • 2015-02-05
    • 2012-10-18
    • 1970-01-01
    相关资源
    最近更新 更多