I'm writing an app that allows users to login via Facebook or Google+. What I've done is write an interface for the login, so that either one can be used. Facebook login went just fine after hours of piecing together their documentation. Google+ on the other hand has me baffled, or maybe I've just been staring at it too long. I can generally find resolutions to my problems on SO, but it's been over a day of searching for an answer.
I implemented the Getting Started with Google+ Platform line by line. I ran my application and I got the page that asked me to approve my app to access my Google+ account. I click Approve and my app crashes. :facepalm: I didn't add the Google+ API in console. I was already using Google Maps, so I had skipped that part.
Now what happens? I have an infinite loop of connection attempts resulting in Toasts saying "An internal error has occurred". I no longer get a Google+ approval screen. I've tried the following solutions from SO 15762904:
- Generated a new console key (even though Google Maps is working fine)
- Filled out my Consent Screen with email and product name
- Lots of answers involve removing the .setScopes() from the deprecated PlusClient API, doesn't apply here
- Double checked that the SHA fingerprint in the Developer Console is the same that was used to generate the API key
- Logged out of Google+ on the device
Here is the main activity code. mSession is the variable I use for logins sessions. I don't have buttons setup right now, I generally go for functionality before GUI.
private Login mSession;
/**************************************************************************
* Activity life cycle methods
**************************************************************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Populate the main content
if (savedInstanceState == null) {
mSession = new GooglePlusLogin(this);
mSession.openSession();
}
}
@Override
protected void onStart() {
super.onStart();
// Call login session onStart() method
mSession.onStart();
}
@Override
protected void onStop() {
super.onStop();
// Call login session onStop() method
mSession.onStop();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mSession.onActivityResult(requestCode, resultCode, data);
}
Here is the GoogleLogin class that implements the login interface.
public class GooglePlusLogin implements Login, ConnectionCallbacks, OnConnectionFailedListener{
private boolean mIntentInProgress;
private Activity mActivity;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private String mSessionToken;
public GooglePlusLogin(Activity activity) {
mGoogleApiClient = null;
mSessionToken = null;
mIntentInProgress = false;
mContext = activity.getApplicationContext();
mActivity = activity;
}
@Override
public boolean openSession() {
boolean ret = false;
Log.d(Helper.TAG, "openSession()");
try {
if(null != mContext) {
Log.d(Helper.TAG, "null != mContext");
mGoogleApiClient =
new GoogleApiClient.Builder(mContext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
Log.d(Helper.TAG, "mPlusClient = " + mGoogleApiClient);
ret = true;
}
else {
throw new LoginException("GooglePlusLogin context NULL");
}
}
catch(LoginException e) {
Log.d(Helper.TAG, "" + e.toString());
}
Log.d(Helper.TAG, "Returning " + ret);
return ret;
}
@Override
public void onActivityResult(int reqCode, int respCode, Intent intent) {
Log.d(Helper.TAG, "onActivityResult()");
if(Helper.GOOGLE_SIGN_IN == reqCode) {
mIntentInProgress = false;
if(!mGoogleApiClient.isConnected()) {
Log.d(Helper.TAG, "Not connected to Google Services, try again");
mGoogleApiClient.connect();
}
else {
Log.d(Helper.TAG, "Already connected to Google Services");
}
}
}
@Override
public void onStart() {
Log.d(Helper.TAG, "onStart()");
mGoogleApiClient.connect();
}
@Override
public void onStop() {
Log.d(Helper.TAG, "onStop()");
if(mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/*************************************************************************
* Google+ Interface callbacks for GoogleApiClient
*************************************************************************/
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.d(Helper.TAG, "onConnectionFailed()");
if(!mIntentInProgress && result.hasResolution()) {
try {
Log.d(Helper.TAG, "Try a Resolution");
mIntentInProgress = true;
//mActivity.startIntentSenderForResult(
// result.getResolution().getIntentSender(),
// Helper.GOOGLE_SIGN_IN,
// null,
// 0,
// 0,
// 0,
// null
// );
result.startResolutionForResult(mActivity, Helper.GOOGLE_SIGN_IN);
}
catch(SendIntentException e) {
// Intent got canceled during processing, lets try and connect
// again.
Log.d(Helper.TAG, "Connect again");
mIntentInProgress = false;
mGoogleApiClient.connect();
}
}
else {
// Something terrible has happened with our connection to
// GooglePlay Services
}
}
@Override
public void onConnected(Bundle connHint) {
// Do my stuff, but I never get to this point
}
@Override
public void onConnectionSuspended(int arg0) {
Log.d(Helper.TAG, "onConnectionSuspended()");
// The connection was suspended... lets try again
mGoogleApiClient.reconnect();
}
Here are the pertinent sections of my AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/app_id_facebook"
/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/app_id_google"
/>
Here's the logcat output:
07-04 11:08:25.354: D/ App(15944): openSession()
07-04 11:08:25.354: D/ App(15944): null != mContext
07-04 11:08:25.364: D/ App(15944): mPlusClient = com.google.android.gms.common.api.b@42238088
07-04 11:08:25.364: D/ App(15944): Returning true
07-04 11:08:25.364: D/ App(15944): Main AuthToken: null
07-04 11:08:25.364: D/ App(15944): onStart()
07-04 11:08:25.615: D/ App(15944): onConnectionFailed()
07-04 11:08:25.615: D/ App(15944): Try a Resolution
07-04 11:08:27.106: D/ App(15944): onActivityResult()
07-04 11:08:27.106: D/ App(15944): Not connected to Google Services, try again
07-04 11:08:27.176: D/ App(15944): onConnectionFailed()
07-04 11:08:27.176: D/ App(15944): Try a Resolution
07-04 11:08:28.538: D/ App(15944): onActivityResult()
07-04 11:08:28.538: D/ App(15944): Not connected to Google Services, try again
07-04 11:08:28.668: D/ App(15944): onConnectionFailed()
07-04 11:08:28.668: D/ App(15944): Try a Resolution
07-04 11:08:29.248: D/ App(15944): onActivityResult()
07-04 11:08:29.248: D/ App(15944): Not connected to Google Services, try again
07-04 11:08:29.479: D/ App(15944): onConnectionFailed()
07-04 11:08:29.479: D/ App(15944): Try a Resolution
07-04 11:08:30.970: D/ App(15944): onStop()
I hope this is something simple that I'm just overlooking. The Get Started guide seemed to have some outdated information in it:
- mGoogleApiClient.addScope(Plus.API, null) crashes my app
- result.getSenderIntent() does not exist, you can see the two ways I've attempted to create the activity.
Much appreciation to anyone who can help.