1

好的,我正在使用Android工作室創建應用程序,並將其與Google遊戲服務鏈接。但是我遇到了一個問題。連接到Google Play遊戲時出錯

出於某種原因,我無法將我的應用連接到Google遊戲。我相信我所做的一切都是正確的。 這是我目前擁有的代碼:

mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Plus.API) 
      .addApi(Games.API) 
      .addScope(Games.SCOPE_GAMES) 
      .addScope(Plus.SCOPE_PLUS_LOGIN) 
      .build(); 

這似乎陷入在一個循環,它無法登錄,然後再次嘗試並重新登錄。 這是我得到的logcat的錯誤:

GoogleApiClient connection failed: ConnectionResult{statusCode=SIGN_IN_REQUIRED, 
resolution=PendingIntent{e154144: [email protected]}} 

然而,當我刪除.addApi(Games.API).addScope(Games.SCOPE_GAMES)遊戲連接到谷歌加完美!

首先,我創建了兩個客戶端ID。一個用於調試,一個用於發佈。 SHA1指紋是正確的。我已檢查並確認兩者匹配。兩者都啓用深度鏈接。我已將gmail添加爲測試人員。 :) 當我檢查谷歌開發者控制檯時,它說已經有81個請求被調用,70個失敗。但是,它不會告訴我哪些失敗,也不知道如何解決它們。我還添加了所有元數據標籤。

請幫忙,問我留下的任何問題。 謝謝 傑克遜·韋爾奇

更新:onActivityResult()

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
    case REQUEST_CODE_RESOLUTION: 
     retryConnecting(); 
     break; 
    } 
} 

這裏是我的onConnectionFailed方法

@Override 
    public void onConnectionFailed(ConnectionResult result) { 
     Log.i(TAG, "GoogleApiClient connection failed: " + result.toString()); 
     if (!result.hasResolution()) { 
      // Show a localized error dialog. 
      GooglePlayServicesUtil.getErrorDialog(
        result.getErrorCode(), this, 0, new OnCancelListener() { 
         @Override 
         public void onCancel(DialogInterface dialog) { 
          retryConnecting(); 
         } 
        }).show(); 
      return; 
     } 

     // If there is an existing resolution error being displayed or a resolution 
     // activity has started before, do nothing and wait for resolution 
     // progress to be completed. 
     if (mIsInResolution) { 
      return; 
     } 
     mIsInResolution = true; 
     try { 
      result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
     } catch (SendIntentException e) { 
      Log.e(TAG, "Exception while starting resolution activity", e); 
      retryConnecting(); 
     } 
    } 
+0

'SIGN_IN_REQUIRED (顯然:用戶需要登錄Google Play遊戲) - 您的「onActivityRes」是什麼? ult()'看起來像? – ianhanniballake

+0

@Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); switch(requestCode){ case REQUEST_CODE_RESOLUTION: retryConnecting(); 休息; } } –

+0

好的,retryConnecting()會做什麼?請編輯您的問題以添加代碼:在評論中閱讀它們非常困難。 – ianhanniballake

回答

1

我解決了這一問題。我用老的谷歌開發者控制檯和舊的將不能工作。重建在新的一個項目,它工作正常!謝謝你們的幫助。

+1

是的,有一個大的紅色框說不通過API控制檯創建它們,但使用播放控制檯:https://developers.google.com/games/services/console/enabling#step_3_generate_an_oauth_20_client_id –

+0

是啊...我忽略了這一點,並沒有認爲這很重要......噢。學過的知識 –

1

確保您正確遵循了handling connection failures。注意onConnectionFailed()例如:

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    if (mResolvingError) { 
     // Already attempting to resolve an error. 
     return; 
    } else if (result.hasResolution()) { 
     try { 
      mResolvingError = true; 
      result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR); 
     } catch (SendIntentException e) { 
      // There was an error with the resolution intent. Try again. 
      mGoogleApiClient.connect(); 
     } 
    } else { 
     // Show dialog using GooglePlayServicesUtil.getErrorDialog() 
     showErrorDialog(result.getErrorCode()); 
     mResolvingError = true; 
    } 
} 

這可確保當你得到一個錯誤,如SIGN_IN_REQUIRED(恰好有result.hasResolution() == true,你會開始糾正錯誤需要適當的活動(即,顯示提示符號)。

+0

檢查我加什麼謝謝! –

相關問題