问题
I am trying to connect my app to Google Play Services but, as I can check, it seems not to connect it properly. When I click the button which checks if it is connected it returns me that is not connected. However the app accesses to the Google Play Services and lets me to check which e-mail account to use.
Here is the code:
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");
}
}
}
This is my 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'
}
and this is my 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" />
...
Am I missing something? I am using for this testing the same account as I registered in Google Developers Console. Am I missing OAuth2 signup?
回答1:
Yes i think you are missing OAuth2 ID for authentication. From documentation:
Your game must have an OAuth 2.0 client ID in order to be authenticated and authorized to call the Google Play games services. To set up the association between a client ID and your game, use the Google Play Developer Console to generate the client ID and link it to your game.
https://developers.google.com/games/services/console/enabling
回答2:
I solved this in 2 hours, but I forgot to answer. Yes I tried the solution provided and it worked. I created the SHA-1 key by using by reference this thread: How can I find and run the keytool, I also used in Google Developer console the same package name as my project and I connect to Google Play Services using the same google account as I registered in Developer Console. So works fine ;)
来源:https://stackoverflow.com/questions/38677245/google-play-services-fails-to-connect