SIGN_IN_REQUIRED - Google游戏

时间:2016-11-15 16:33:39

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

我有一个简单的游戏,有一个排行榜按钮,它显示了玩家的最高分,在发布此问题之前,我确定了以下内容:

  1. SHA1指纹。
  2. 启用了API。
  3. 正确包装。
  4. 更正应用程序ID。
  5. 导入并添加了Google Play服务库。
  6. 导入配置文件。
  7. Admob,Google Analytics和崩溃报告工作正常。
  8. GET ACCOUNTS已添加,仍然相同。
  9. 我的问题是,没有显示帐号选择器/选择器,甚至没有登录,我收到以下错误:

    ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{fbd982b: android.os.BinderProxy@b3d396d}, message=null
    

    代码:

    public class MainActivity extends AppCompatActivity implements
        GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    
    ImageButton mLeaderBoard;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.activity_welcome);
        mLeaderBoard = (ImageButton) findViewById(R.id.leaderboard);
        mLeaderBoard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowBoard();
            }
        });
    
       mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Games.API)
                .addScope(Games.SCOPE_GAMES)
                .addScope(SCOPE_PLUS_PROFILE)
                .build();
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }
    
    private void ShowBoard() {
        if(!isSignedIn()){
            mGoogleApiClient.connect();
        }
        mGoogleApiClient.connect();
        if(isSignedIn())
        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,
                LEADERBOARD_TOP_SCORE_ID), 1);
        else
            Toast.makeText(this, getResources().getString(R.string.leaderboard_not_available), Toast.LENGTH_LONG).show();
    
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check which request we're responding to
        if (requestCode == 1) {
    
            Log.i("GoogleSignInApi","Result: " + resultCode + " - Data: " + data.getData());
            if (resultCode == RESULT_OK) {
    
            }
        }
    }
    
    
    
    @Override
    public void onConnected(Bundle bundle) {
    
    }
    
    @Override
    public void onConnectionSuspended(int i) {
        mGoogleApiClient.connect();
    }
    
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Log.i("GoogleSignInApi","Problem: " + connectionResult.getErrorMessage() + " - Test: " + connectionResult.toString());
    }
    
     }
    

2 个答案:

答案 0 :(得分:1)

AFAIK,您应该有SCOPE_PLUS_PROFILE - OAuth2.0范围来访问用户的个人资料数据。您需要启用Google+ API并使用SHA1和您的包创建凭据。 Oauth Consent屏幕显示您的电子邮件和产品名称。

确保您拥有所需的权限和所有包,例如:

applicationId in tag <application> 
package name in AndroidManifest.xml 
package name in Credentials in Google Developer Console

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> 

此外,您需要致电onActivityResult。当客户端无法连接到Google Play服务时,需要此功能来满足所有可能的错误代码。

答案 1 :(得分:0)

就我而言,即使用户以前从未使用过Google来登录我的应用程序,我仍试图从Google注销

const signInGoogle = async () => {
    try {
      await GoogleSignin.hasPlayServices();
      const userInfo = await GoogleSignin.signIn();
      console.log(userInfo);
      const payload = {
        token: userInfo.idToken,
        first_name: userInfo.user.givenName,
        last_name: userInfo.user.familyName,
        email: userInfo.user.email,
      };
      props.actions.socialLogin(payload);
      console.warn('google', payload);
    } catch (error) {
      if (error.code === statusCodes.SIGN_IN_CANCELLED) {
        // user cancelled the login flow
        alert('Cancel');
      } else if (error.code === statusCodes.IN_PROGRESS) {
        alert('Signin in progress');
        // operation (f.e. sign in) is in progress already
      } else if (error.code === statusCodes.PLAY_SERVICES_NOT_AVAILABLE) {
        alert('PLAY_SERVICES_NOT_AVAILABLE');
        // play services not available or outdated
      } else {
        console.log(error);
      }
    }
  }
  const handleGoogle = async () => {

    try{
      await GoogleSignin.revokeAccess();
      await GoogleSignin.signOut();
      signInGoogle()
    }catch(e){
      //HERE IT Throws the SIGN_IN_REQUIRED ERROR DUE TO signOut function
      signInGoogle()
    }
  };