再加上谷歌为Android:凭据无效 [英] Google plus for android: Invalid Credentials

查看:176
本文介绍了再加上谷歌为Android:凭据无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是跟随着code来获得访问令牌,连结Google+后,拿到个人资料信息和电子邮件:

I am using the followin code to get an Access Token, after connecting to Google+, to get profile info and e-mail:

String sAccessToken = GoogleAuthUtil.getToken(this,mPlusClient.getAccountName() + "",
                          "oauth2:" + Scopes.PLUS_PROFILE + " 
                          https://www.googleapis.com/auth/userinfo.profile 
                          https://www.googleapis.com/auth/userinfo.email");

此访问令牌,我不存储,并试图重新使用。相反,我问一个访问令牌每次,期待一个新的(不同的)令牌,所以我不需要检查其是否过期等。

This Access Token I am not storing and trying to re-use. Instead I ask for an Access Token everytime, expecting a new(different) Token, so that I don't need to check whether its expired etc..

但我正在逐渐每次我问时间相同的访问令牌。我卸载应用程序,清除了数据的 Google+应用程序并仍然我得到同样的访问令牌。

But I am getting the same Access Token every time I ask for. I uninstalled the app, cleared data of Google+ App and still I get the same Access Token.

在实际的问题是,使用这个访问令牌给一个401错误 - 消息:无效凭证

The Actual problem is, using this Access Token gives a 401 error - "message": "Invalid Credentials"

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

这是对浏览器的响应。

在设备上我得到这个在logcat中:

On a device I get this in Logcat:

02-25 02:09:46.919: W/System.err(3022): java.io.FileNotFoundException: https://www.googleapis.com/oauth2/v1/userinfo
02-25 02:09:46.929: W/System.err(3022):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
02-25 02:09:46.929: W/System.err(3022):     at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:258)
02-25 02:09:46.929: W/System.err(3022):     at com.example.gmaillogin.MainActivity$connectAsyncTask.doInBackground(MainActivity.java:269)
02-25 02:09:46.929: W/System.err(3022):     at com.example.gmaillogin.MainActivity$connectAsyncTask.doInBackground(MainActivity.java:1)
02-25 02:09:46.929: W/System.err(3022):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-25 02:09:46.929: W/System.err(3022):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-25 02:09:46.929: W/System.err(3022):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-25 02:09:46.929: W/System.err(3022):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-25 02:09:46.929: W/System.err(3022):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-25 02:09:46.929: W/System.err(3022):     at java.lang.Thread.run(Thread.java:1019)

269线:

urlConnection = (HttpURLConnection) url.openConnection();

完成code:

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo");
String sAccessToken = GoogleAuthUtil.getToken(
                        MainActivity.this,
                        mPlusClient.getAccountName() + "",
                        "oauth2:" + Scopes.PLUS_PROFILE + " 
                                            https://www.googleapis.com/auth/userinfo.profile 
                                            https://www.googleapis.com/auth/userinfo.email");

            Log.d("sAccessToken = ", "" + sAccessToken);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Bearer "
                    + sAccessToken);

            BufferedReader r = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream(), "UTF-8"));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            line = total.toString();

编辑:

这肯定是因为访问令牌已过期。预计这,但我想我会得到一个新的访问令牌,每次我打电话。

This is definitely because the Access Token is expired. Which was expected, but I thought I will get a new Access Token everytime I call.

但是作为所述<一href=\"http://stackoverflow.com/questions/9527751/authenticating-with-google-api-using-accountmanager#answers-header\">here:

如果Android设备已经有一个验证令牌(为特定
  你试图访问)的GData服务,也将退还给你。

If the android device already has an auth token (for the particular GData service you're trying to access), it will be returned to you

所以,我必须这样做:

if (server indicates token is invalid) {
              // invalidate the token that we found is bad so that GoogleAuthUtil won't
              // return it next time (it may have cached it)
              GoogleAuthUtil.invalidateToken(Context, String)(context, token);
              // consider retrying getAndUseTokenBlocking() once more
              return;
          }

像<一个href=\"http://developer.android.com/reference/com/google/android/gms/auth/GoogleAuthUtil.html\">docs说

但我怎么能检查是否访问令牌已过期或无效?

But how can I check if the Access Token is expired or invalid?

通过捕获异常? - 是唯一的解决办法

By catching the Exception - Is the only solution?

其实例外是 java.io.FileNotFoundException 这是没有意义的。

Actually the Exception is java.io.FileNotFoundException which doesn't make sense.

推荐答案

下面覆盖的基础:你的注册一个谷歌的API控制台项目并创建的OAuth 2.0客户端ID为您的Andr​​oid应用程序,通过指定你的应用程序的包名称和证书(测试或正式版)?

Covering the bases here: Did you register a Google APIs Console project and create a OAuth 2.0 client ID for your Android app, by specifying your app's package name and the SHA-1 fingerprint of your certificate (test or prod)?

通常情况下,401凭证无效的消息是由于API控制台项目要么没有被配置或设置一个是无效的。

Often the 401 invalid credentials message is due to the APIs Console project either not being configured or one of the settings being invalid.

通过专门为Google+的SDK的设置过程走的步骤是在这里:的 https://developers.google.com/+/mobile/android/getting-started

The steps to walk through that set up process specifically for the Google+ SDK are here: https://developers.google.com/+/mobile/android/getting-started

修改

PlusClient 可以自动处理您的令牌你。我建议使用的 PlusClient 提供的方法和允许客户端来管理令牌,而不是做手工。如果你需要通过 PlusClient PlusClient.loadPerson()和电子邮件 PlusClient.getAccountName(),在 HTTPS的更多详细信息:// developers.google.com/+/mobile/android/people

The PlusClient can automatically handle your tokens for you. I'd suggest using the methods that the PlusClient provides and allowing the client to manage the tokens rather than doing it manually. If you need email or profile data via the PlusClient see PlusClient.loadPerson() and for email PlusClient.getAccountName(), more details at https://developers.google.com/+/mobile/android/people

这篇关于再加上谷歌为Android:凭据无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆