Created
March 17, 2014 20:22
-
-
Save ianbarber/9607551 to your computer and use it in GitHub Desktop.
A quick example of retrieving an access token with GoogleAuthUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.google.devrel.samples.gmstest.app; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.google.android.gms.auth.GoogleAuthException; | |
import com.google.android.gms.auth.GoogleAuthUtil; | |
import com.google.android.gms.auth.UserRecoverableAuthException; | |
import com.google.android.gms.plus.Account; | |
import com.google.android.gms.plus.Plus; | |
import java.io.IOException; | |
public class RetrieveAccessTokenActivity extends Activity implements View.OnClickListener { | |
private static final String TAG = "RetrieveAccessToken"; | |
private static final int REQ_SIGN_IN_REQUIRED = 55664; | |
private String mAccountName; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_retrieve_access_token); | |
findViewById(R.id.button_token).setOnClickListener(this); | |
// Manual integration? Pop an account chooser to get this: | |
mAccountName = "[email protected]"; | |
// Or if you have a GoogleApiClient connected: | |
// mAccountName = Plus.AccountApi.getAccountName(mGoogleApiClient); | |
} | |
@Override | |
public void onClick(View view) { | |
if (view.getId() == R.id.button_token) { | |
new RetrieveTokenTask().execute(mAccountName); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (requestCode == REQ_SIGN_IN_REQUIRED && resultCode == RESULT_OK) { | |
// We had to sign in - now we can finish off the token request. | |
new RetrieveTokenTask().execute(mAccountName); | |
} | |
} | |
private class RetrieveTokenTask extends AsyncTask<String, Void, String> { | |
@Override | |
protected String doInBackground(String... params) { | |
String accountName = params[0]; | |
String scopes = "oauth2:profile email"; | |
String token = null; | |
try { | |
token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes); | |
} catch (IOException e) { | |
Log.e(TAG, e.getMessage()); | |
} catch (UserRecoverableAuthException e) { | |
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED); | |
} catch (GoogleAuthException e) { | |
Log.e(TAG, e.getMessage()); | |
} | |
return token; | |
} | |
@Override | |
protected void onPostExecute(String s) { | |
super.onPostExecute(s); | |
((TextView) findViewById(R.id.token_value)).setText("Token Value: " + s); | |
} | |
} | |
} |
Worked.But with that Access token i could not be able to receive the Contact list .
Please give me solution if you can.
Thanks in advance
@ianbarber thank you very much.That really helped #
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked like a charm ... thank you so much.