0

私のアプリをGoogle Playサービスに接続しようとしていますが、確認すると正しく接続されていないようです。それが接続されているかどうかをチェックするボタンをクリックすると、接続されていない私を返します。ただし、アプリはGoogle Playサービスにアクセスし、使用するメールアカウントを確認することができます。ここでGoogle Playサービスの接続に失敗しました

は、コードは次のとおりです。

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener { 

/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 
//GoogleApiClient 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    client = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(Games.API).addScope(Games.SCOPE_GAMES) 
      .build(); 
    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      //Games.signOut(client); 
      TextView textView = (TextView) findViewById(R.id.helloworld); 
      //client.connect(); 
      if(client.isConnected()){ 
       textView.setText("Buttonconnected"); 

      }else{ 

       textView.setText("Buttonnotconnected"); 
      } 
     } 
    }); 


    if (client == null) { 

    } 
} 

@Override 
public void onStart() { 
    super.onStart(); 
    //Log.i("app","Lapp"); 
    if (client.isConnected()) { 
     //TextView t = (TextView) findViewById(R.id.helloworld); 
     //t.setText("Connected"); 
    } 
    /* GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); 
    int resultCode = googleApiAvailability.isGooglePlayServicesAvailable(this); 
    if (resultCode == ConnectionResult.SUCCESS) { 
     Toast.makeText(this, "Google Play Works!!", Toast.LENGTH_LONG).show(); 
    } else{ 
     Toast.makeText(this, "Google Play Services required!!", Toast.LENGTH_LONG).show(); 
    }*/ 
    if (client != null) { 
     client.connect(); 
    } 

    if (client.isConnected()) { 
     TextView t = (TextView) findViewById(R.id.helloworld); 
     t.setText("Connected"); 
    } 

} 

@Override 
public void onStop() { 
    super.onStop(); 

} 

public void onConnectionSuspended(int cause) { 
    // We are not connected anymore! 
    Log.i("app", "connectionsuspended"); 
} 

public void onConnected(Bundle connectionHint) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult result) { 
    // We tried to connect but failed! 
    TextView t; 
    //  t = (TextView) findViewById(R.id.helloworld); 
      //  t.setText("Failed"); 


    try { 
     //result.startResolutionForResult(this, 49404); 
     result.startResolutionForResult(this, 1001); 
     Log.i("app", "show"); 
    } catch (Exception e) { 
     Log.i("app", e.toString()); 
    } 


    client.connect(); 
    client.hashCode(); 
    Toast.makeText(this, "Toasting", Toast.LENGTH_LONG).show(); 
    if (client.isConnected()) { 
     Log.i("app", "worked"); 
     t = (TextView) findViewById(R.id.helloworld); 
     t.setText("FailedConnected"); 
    } 
    if (client.isConnecting()) { 
     t = (TextView) findViewById(R.id.helloworld); 
     t.setText("Connecting"); 
     Log.i("app", "Failedtryingtoconnect"); 

    } 


} 

} 

これは私のbuild.gradleです....

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.android.support:appcompat-v7:24.0.0' 
compile 'com.google.android.gms:play-services:9.0.2' 
} 

、これが私たManifest.xml

です。 ...

<meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
    <meta-data android:name="com.google.android.gms.games.APP_ID" 
     android:value="@string/app_id" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> 

...

私に何か不足していますか?このテストでは、Google Developers Consoleに登録したのと同じアカウントを使用しています。 OAuth2のサインアップがありませんか?

答えて

2

はい私はOAuth2 IDが認証に欠けていると思います。 ドキュメントから:

あなたのゲームは を認証され、Googleがゲームサービスをプレイ呼び出すことを許可するために、OAuth 2.0のクライアントIDを持っている必要があります。 クライアントIDとゲームの関連付けを設定するには、 Google Playデベロッパーコンソールを使用してクライアントIDを生成し、ゲームに にリンクします。

https://developers.google.com/games/services/console/enabling

+1

私は私のアプリのIDは、アプリの名前の隣にある数字だと思った。私は適切なOAuth2トークンを生成して再試行しようとします、ありがとう、 – NUN

0

私は2時間でこれを解決したが、私は答えるのを忘れていました。 はい提供されたソリューションを試してみました。 How can I find and run the keytoolを使用してSHA-1キーを作成しました。また、Googleデベロッパーコンソールでプロジェクトと同じパッケージ名を使用しました。デベロッパーコンソールに登録したのと同じGoogleアカウントを使用してGoogle Playサービスに接続します。だからうまくいく;)

関連する問題