0

我試圖在Android上獲取身份驗證令牌,該身份驗證令牌可以在我的網絡服務器上交換爲accessToken和RefreshToken。 我正在關注this documentation
具體的部分 - Android應用程序獲得的Web後端離線訪問無法使用GoogleAuthUtil.getToken獲取身份驗證令牌

我的代碼:

String WEB_APP_CLIENT_ID = "***.apps.googleusercontent.com"; 
String scopes = "oauth2:server:client_id:" + WEB_APP_CLIENT_ID+":api_scope:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read"; 
//String this_scope_works_but_gives_access_token = "oauth2:" + Scopes.PLUS_LOGIN + " https://www.googleapis.com/auth/plus.profile.emails.read"; 
try { 
    String authToken = GoogleAuthUtil.getToken(AttachEmailActivity.this, account,scopes); 
} catch (GoogleAuthException e) { 
    e.printStackTrace(); 
} 

但它總是給我以下異常:

com.google.android.gms.auth.GoogleAuthException: Unknown 
com.google.android.gms.auth.GoogleAuthUtil.zza(Unknown Source) 
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 
com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 
app.activities.AttachEmailActivity$1.run(AttachEmailActivity.java:437) 

我已閱讀並試圖幾乎所有解決類似問題的解決方案仍然失敗。我曾嘗試使用所有不同的客戶端ID - 包括Web客戶端ID,服務器應用程序ID,Android客戶端ID,其他客戶端ID

事實上,我使用相同的Web客戶端ID在我的JAVA應用程序之外使用了rest API,獲得所需的認證碼。

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id= ****。apps.googleusercontent.com & REDIRECT_URI = http://localhost&scope=https://www.googleapis.com/auth/plus.login&access_type=offline

但在Java應用程序中,我只得到 「未知」 的例外。 不能谷歌提供更多信息?什麼是未知數?有沒有辦法獲取com.google.android.gms.auth包代碼並查看拋出異常的位置?

Here is the view of credentials dashboard

+0

您是否已將密鑰註冊到Google Developer API控制檯? –

+0

Niko - 我已將我的憑證儀表板圖像添加爲問題的一部分。 –

回答

0

對我來說,我需要捕捉UserRecoverableAuthException,要求用戶權限

String scopes = "oauth2:server:client_id:" 
     + 
     getResources().getString(R.string.google_auth_client_id) 
     + 
     ":api_scope:" + Scopes.PLUS_LOGIN; 
try { 
    String token = GoogleAuthUtil.getToken(MyActivity.this, 
      Plus.AccountApi.getAccountName(googleApiClient), scopes); 
    subscriber.onNext(token); 
} catch (UserRecoverableAuthException e) { 
    startActivityForResult(e.getIntent(), 888); 
    e.printStackTrace(); 
} catch (GoogleAuthException | IOException e) { 
    e.printStackTrace(); 
} 

然後拿到令牌裏面onActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 888) { 
     if (data != null && resultCode == Activity.RESULT_OK) { 
      Bundle extra = data.getExtras(); 
      String token = extra.getString(getResources() 
        .getString("authtoken")); 
      // Do something with your token 
     } 

     if (!googleApiClient.isConnecting()) { 
      googleApiClient.connect(); 
     } 
    } 
} 
+0

代碼看起來類似,但對我來說問題出現在getToken調用本身。如果我使用更簡單的範圍字符串(即沒有server:client_id「),它將顯示驗證字符串並返回accessToken。 –

0

檢查下面

以下1.檢查註冊關鍵是工作與否?

2.檢查您的密鑰調試或簽名apk。

3.如果Signatured apk的關鍵,它不適用於調試模式。

4.so你應該創建signatured apk並安裝在你的android手機上,並檢查它是否有效?

5.當您通過Android應用程序中的Gmail配置文件登錄時,是否有popoup(如下圖)?

6.如果您在選擇Gmail帳戶時沒有彈出(同意屏幕)。當然問題是您的註冊密鑰

enter image description here

+0

我正在通過更簡單的作用域字符串獲得同意屏幕:「oauth2:」+ Scopes.PLUS_LOGIN +「https://www.googleapis.com/auth/plus.profile.emails.read」;但與另一個,我得到了例外。和我可以在瀏覽器中使用的相同client_id使用其餘API –

+0

我正在使用signatured apk並在我的設備上進行測試。也許我需要重新開始。 :( –

+0

你是否有Auth令牌?並且檢查你的Android手機互聯網連接 –

相關問題