简体   繁体   中英

Google + Sign in wont find users account

after signing in with google + sign in it asks me if I want a public or if I only want to view it. I say public or the other one and it signs me in. I then check my allowed applications in google and it shows me my application on google, So I connected.

But after connecting my entire application freezes because in the work log it tells me problem locating account. I don't know why this keeps happening and I cant find out the reason why. In order to fix it I have to log into google and disconnect my Application from the allowed application list in google not my app. heres the code

its not the full code of my application but it is the full google sign in code that I used

Java Code

  public class mainMenu extends Activity implements ConnectionCallbacks, OnConnectionFailedListener, OnClickListener {
private static final int REQUEST_CODE_RESOLVE_ERR = 9000;
private ProgressDialog mConnectionProgressDialog;
private PlusClient mPlusClient;
private ConnectionResult mConnectionResult;
private static final String TAG = "Back To School";
static final String[] SCOPES = new String[] {Scopes.PLUS_PROFILE};



      @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);  //This is calling the layout in the main.xml file if I change the main.xml file name then I would 
    mPlusClient = new PlusClient.Builder(this, this, this)
    .setVisibleActivities("http://schemas.google.com/AddActivity", "http://schemas.google.com/BuyActivity").build();
    // Progress bar to be displayed if the connection failure is not resolved.
     mConnectionProgressDialog = new ProgressDialog(this);
     mConnectionProgressDialog.setMessage("Signing in...");
      findViewById(R.id.sign_in_google).setOnClickListener(this);


@Override
protected void onStart() {
super.onStart();
mPlusClient.connect();
}


      @Override
      protected void onStop() {
      super.onStop();
      mPlusClient.disconnect();
      }

       public void onConnectionFailed(ConnectionResult result) {
       if (mConnectionProgressDialog.isShowing()) {
       // The user clicked the sign-in button already. Start to resolve
       // connection errors. Wait until onConnected() to dismiss the
       // connection dialog.
      if (result.hasResolution()) {
      try {
       result.startResolutionForResult(this, REQUEST_CODE_RESOLVE_ERR);
      } catch (SendIntentException e) {
       mPlusClient.connect();
      }
    }
  }
   // Save the result and resolve the connection failure upon a user click.
  mConnectionResult = result;
 }

 @Override
   protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
 if (requestCode == REQUEST_CODE_RESOLVE_ERR && responseCode == RESULT_OK) {
  mConnectionResult = null;
  mPlusClient.connect();
  }
}
 public void onConnected(Bundle connectionHint) {
  String accountName = mPlusClient.getAccountName();
 Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG).show();
 }

  public void onDisconnected() {
   Log.d(TAG, "disconnected");
     }

XML Code

 <com.google.android.gms.common.SignInButton
   android:id="@+id/sign_in_google"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
                                            >

anyone know why it freezes after signing in and allowing it into my google sign in.

Btw I already did the client id part which is below but I couldn't figure out what a scope is so its commented out for now

Java Code not used at the moment

   // Prior to disconnecting, run clearDefaultAccount().
   /*
  mPlusClient.clearDefaultAccount();

  mPlusClient.revokeAccessAndDisconnect(new OnAccessRevokedListener() {
  public void onAccessRevoked(ConnectionResult status) {
   // mPlusClient is now disconnected and access has been revoked.
   // Trigger app logic to comply with the developer policies
  }
});


    Bundle appActivities = new Bundle();
    appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
      "<app-activity1> <app-activity2>");
     String scopes = "oauth2:server:client_id:44890425655.apps.googleusercontent.com:api_scope:<scope1> <scope2>";
    @SuppressWarnings("unused")
    String code = null;
    try {
    code = GoogleAuthUtil.getToken(
     this,                             // Context context
     mPlusClient.getAccountName(),     // String accountName
scopes,                           // String scope
     appActivities                     // Bundle bundle
     );

    } catch (IOException transientEx) {
  // network or server error, the call is expected to succeed if you try again later.
  // Don't attempt to call again immediately - the request is likely to
  // fail, you'll hit quotas or back-off.
  return;
 } catch (UserRecoverableAuthException e) {
  // Recover
 code = null;
 } catch (GoogleAuthException authEx) {
 // Failure. The call is not expected to ever succeed so it should not be
 // retried.

 return;
 } catch (Exception e) {
 throw new RuntimeException(e);
 }*/

Here is the Log Cat

 08-13 22:45:48.970: E/AndroidRuntime(19298): java.lang.SecurityException: Missing android.permission.GET_ACCOUNTS

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at android.os.Parcel.readException(Parcel.java:1425)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at android.os.Parcel.readException(Parcel.java:1379)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.dx$a$a.getAccountName(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.dy.getAccountName(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.plus.PlusClient.getAccountName(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.better.work.learning.letters.and.more.awesome.mainMenu.onConnected(mainMenu.java:218)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.p.k(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.p$f.a(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.p$f.a(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.p$b.p(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.google.android.gms.internal.p$a.handleMessage(Unknown Source)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at android.os.Handler.dispatchMessage(Handler.java:99)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at android.os.Looper.loop(Looper.java:137)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at android.app.ActivityThread.main(ActivityThread.java:4898)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at java.lang.reflect.Method.invokeNative(Native Method)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at java.lang.reflect.Method.invoke(Method.java:511)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)

 08-13 22:45:48.970: E/AndroidRuntime(19298):   at dalvik.system.NativeStart.main(Native Method)

I referred to this link Android Permissions

I was able to solve my own question because I never went into the manifest and added this line here

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

Adding this line stops the application from crashing because its able to locate the account and store them and then continue to connect without that line it wont get the account and will fail on the first part of your code Get Account code therefore causing your application to crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM