Google Play服务:无法连接到服务器

时间:2014-10-25 20:14:23

标签: java android google-play google-play-services

(背景:我想要的只是用户的唯一且持久的Google Play标识符。(即使在卸载后,或在不同的设备上)这是我这样做的唯一原因。)

我正在使用Cordova。这是我的主要活动。

问题:onConnected函数永远不会运行。但是,我可以签到罚款。 (我可以看到登录窗口,登录圈子以及其他所有内容)但它永远不会运行。

注意:onConnectionFailed运行一次,带有SIGN_IN_REQUIRED statusCode。

package com.myapp;

import android.content.Intent;
import android.content.IntentSender.SendIntentException;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;

import com.google.android.gms.games.Players;
import com.google.android.gms.games.Games;

import android.os.Bundle;
import org.apache.cordova.*;

import android.util.Log;

public class MyApp extends CordovaActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
 private static final String LOGTAG = "GooglePlayServices";

 // Client used to interact with Google APIs.
 private GoogleApiClient mGoogleApiClient;
 private CordovaActivity activity;
 boolean mResolvingError;

 @Override public void onCreate (Bundle savedInstanceState) {
  activity = this;
  super.onCreate (savedInstanceState);
  super.init ();
  mGoogleApiClient = new GoogleApiClient.Builder (this)
   .addConnectionCallbacks (this)
   .addOnConnectionFailedListener (this)
   .addApi (Games.API)
   .addScope(Games.SCOPE_GAMES)
   .build ();
   mGoogleApiClient.connect ();
   super.loadUrl(Config.getStartUrl());
 }
 @Override public void onConnectionFailed (ConnectionResult result) {
  if (mResolvingError) return;
  if (!result.hasResolution()) {mResolvingError = true; return;}
  Log.d (LOGTAG, result.toString());
  try {
   mResolvingError = true;
   result.startResolutionForResult (this, result.getErrorCode());
  } catch (SendIntentException e) {
   // There was an error with the resolution intent. Try again.
   mGoogleApiClient.connect ();
  }
 }
 @Override public void onConnected (Bundle connectionHint) {
  // This never runs... this is the most critical part. I need the player ID!
  String playerId = Games.Players.getCurrentPlayerId (mGoogleApiClient);
  Log.w (LOGTAG, playerId);
 }
 // I saw this one with an @Override in others' code, but it won't compile if I add that.
 public void onDisconnected () {}
 protected void onStart () {super.onStart (); mGoogleApiClient.connect ();}
 protected void onStop () {
  super.onStop ();
  if (mGoogleApiClient.isConnected()) mGoogleApiClient.disconnect ();
 }
 protected void onActivityResult (int requestCode, int responseCode, Intent intent) {
  if (!mGoogleApiClient.isConnecting()) mGoogleApiClient.connect ();
 }
 public void onConnectionSuspended (int cause) {mGoogleApiClient.connect ();}
}

1 个答案:

答案 0 :(得分:0)

“您的Oauth2客户端无法通过Google API控制台创建,必须通过Google Play用户界面创建。”