2016-03-27 15 views
5

私のゲームではGoogle PlayゲームとFirebaseが使用されています(残念ながら、Googleリーダーボードでスコアを下げることはできません)。私がサインインするFirebaseにPlus.AccountApi.getAccountNameや友人を経由して、非推奨の方法を使用し、過去にGoogle Playゲーム、Firebase、新しいGoogleログイン

...

は、だから私は、新しいGoogleサインインに変換しようとしたが、どうやらそれは接続詞で使用することはできませんグーグルでゲームをプレイ:

Auth.GOOGLE_SIGN_IN_API cannot be used with Games.API 

私の全体の目標は、/ Google +の依存関係(Googleがゲームはもうそれを必要としない再生として)とGET_ACCOUNTS許可を取り除くことですました。

新しいGoogleログインがなくても、Google + GET_ACCOUNTSなしでFirebaseにログインするにはどうすればよいですか?私はできますか?おそらく2つの別々のGoogleApiClientを作成することが解決策になりますか?

+0

問題を再現するための最小限のコードは表示されませんが、なぜ機能していないのかは分かりません。 [MCVE](http://stackoverflow.com/help/mcve)を参照してください。 –

答えて

2

まあ、Google Playゲーム用のものと新しいGoogleサインインを使用するFirebase用の別々の2つのGoogleApiClientを作成しただけです。この方法では問題はなく、Google+の依存関係& GET_ACCOUNTS権限を取り除きました。

+0

私は同様の問題に直面しています。私はあなたのユースケースから詳細を学びたいと思います。 2つのconnect()呼び出しをどのように順序付けますか? (私はあなたが2つの別々の接続呼び出しをしなければならないと仮定します) – dlmt

+0

2つの別々のAPIクライアントを使って、ユーザーが自分のデバイスに複数のアカウントを持っている場合、別のGoogleアカウントまたはplaygamesアカウントを選択する問題をどうして防ぎますか?私は簡単に2つが一致しないシナリオを見ることができました。 – Moritz

1

Googleは次の手順を実行しFirebaseでゲームAPIを再生使用するには:

***注:getGamesServerAuthCodeは、Googleからのお勧めの方法です。減価償却されますが。一般公開すると、非推奨の注釈を削除するのを忘れている可能性があります。

手順1: ログインに成功したら、認証コードを取得します。たとえば、onSignInSucceeded()を使用できます。 APIクライアントが接続されていることを確認してください。

Games.getGamesServerAuthCode(gameHelper.getApiClient(), [web_client_id]).setResultCallback(new ResultCallback<Games.GetServerAuthCodeResult>() { 
      @Override 
      public void onResult(@NonNull Games.GetServerAuthCodeResult result) { 
       if (result.getStatus().isSuccess()) { 
        String authCode = result.getCode(); 
        exchangeAuthCodeForToken(authCode); 
       } 
      } 
     }); 

ステップ2:トークンの認証コードを交換します。

class AuthToken { 
    String access_token; 
    String token_type; 
    int expires_in; 
    String id_token; 
} 

void exchangeAuthCodeForToken(final String authCode) { 
    AsyncTask<Void, Void, AuthToken> task = new AsyncTask<Void, Void, AuthToken>() { 
     @Override 
     protected AuthToken doInBackground(Void... voids) { 
      try { 
       URL url = new URL("https://www.googleapis.com/oauth2/v4/token"); 
       Map<String, Object> params = new LinkedHashMap<>(); 
       params.put("code", authCode); 
       params.put("client_id", "[web_client_id]"); 
       params.put("client_secret", "[secret]"); 
       params.put("redirect_uri", "[redirect_uri]"); 
       params.put("grant_type", "authorization_code"); 

       StringBuilder postData = new StringBuilder(); 
       for (Map.Entry<String, Object> param : params.entrySet()) { 
        if (postData.length() != 0) postData.append('&'); 
        postData.append(URLEncoder.encode(param.getKey(), "UTF-8")); 
        postData.append('='); 
        postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8")); 
       } 
       byte[] postDataBytes = postData.toString().getBytes("UTF-8"); 

       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Host", "www.googleapis.com"); 
       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       conn.setDoOutput(true); 
       conn.getOutputStream().write(postDataBytes); 

       int responseCode = conn.getResponseCode(); 
       if (responseCode == HttpsURLConnection.HTTP_OK) { 
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); 

        // Build the string 
        StringBuilder sb = new StringBuilder(); 
        for (int c; (c = in.read()) >= 0;) { 
         sb.append((char) c); 
        } 

        // Convert JSON to a Java Object 
        GsonBuilder builder = new GsonBuilder(); 
        Gson gson = builder.create(); 
        AuthToken token = gson.fromJson(sb.toString(), AuthToken.class); 

        // Disconnect 
        conn.disconnect(); 

        return token; 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(AuthToken token) { 
      // Authorize Firebase with Games API 
      firebaseAuthWithGamesApi(token.id_token); 
     } 

    }.execute(); 
} 

ステップ3:認証コードを使用してFirebaseにサインインします。

private void firebaseAuthWithGamesApi(String authToken) { 
     AuthCredential credential = GoogleAuthProvider.getCredential(authToken, null); 
     mAuth.signInWithCredential(credential) 
       .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful()); 

         // If sign in fails, display a message to the user. If sign in succeeds 
         // the auth state listener will be notified and logic to handle the 
         // signed in user can be handled in the listener. 
         if (!task.isSuccessful()) { 
          Log.w(TAG, "signInWithCredential", task.getException()); 
         } 
        } 
       }); 
    } 
+0

それは解決策ですが、 'Games.getGamesServerAuthCode'は廃止予定です。次のネットワークリクエストは、' id_token'なしでトークン情報を返すので、私たちはfirebaseにログインする必要がある 'AuthCredential'を取得する必要があります – mohax

+0

減価償却されたとマークされ、彼らはそれを削除することを忘れていた。確かに。彼らの交換に取り組んでいると確信しています。しかし、私はまだそれを見ている。 –

+0

'id_token'がないのはどうですか?私はここからいくつかの回答を試みますが、このフィールドをレスポンスで取得することはできません(Ony 'acces_token'が返され、GoogleAuthProvider.getCredentialに渡そうとすると、ここで' id_token'を認識できないというメッセージで失敗します) – mohax

関連する問題