问题
Is a way to access from android the new gmail api by using GoogleApiClient and get messages from account? If is possible can someone give me an example or guiding me? If not what is the best way to get the messages from account by not using imap or smtp.
回答1:
I was facing the same issue, the solution was given on Google development site. this is how it can be done , add scopes as "oauth2:" + GmailScopes.MAIL_GOOGLE_COM u can add muliple scopes comma seperated but you have to use oauth2: before the first scope you enter . this is how i call it
new GetAuthTokenTask(MainActivity.this,
emailId,
"oauth2:" + GmailScopes.MAIL_GOOGLE_COM)
.execute();
and this is the code for Async task
public class GetAuthTokenTask extends AsyncTask<Void, Void, Void> {
private final String LOG_TAG = GetAuthTokenTask.class.getSimpleName();
Activity mActivity;
String mScope;
String mEmail;
GetAuthTokenTask(Activity activity, String name, String scope) {
this.mActivity = activity;
this.mScope = scope;
this.mEmail = name;
}
/**
* Executes the asynchronous job. This runs when you call execute()
* on the AsyncTask instance.
*/
@Override
protected Void doInBackground(Void... params) {
try {
String token = fetchAuthToken();
if (token != null){
Log.d(LOG_TAG, "Token :" + token);
}
} catch (IOException e) {
Log.e(LOG_TAG, " Error Internet Connection :" + e);
e.printStackTrace();
}
return null;
}
protected String fetchAuthToken() throws IOException{
try {
Log.d(LOG_TAG, "accountId :" + mEmail);
return GoogleAuthUtil.getToken(mActivity, mEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
// GooglePlayServices.apk is either old, disabled, or not present
// so we need to show the user some UI in the activity to recover.
Log.e(LOG_TAG, "Error UserRecoverableAuthException :" + userRecoverableException);
userRecoverableException.printStackTrace();
handleException(userRecoverableException);
} catch (GoogleAuthException fatalException) {
// Some other type of unrecoverable exception has occurred.
// Report and log the error as appropriate for your app.
Log.e(LOG_TAG, "Error GoogleAuthException :" + fatalException);
fatalException.printStackTrace();
}
return null;
}
}
add this function to the activity from which you are executing this async task
public void handleException(final Exception e) {
// Because this call comes from the AsyncTask, we must ensure that the following
// code instead executes on the UI thread.
runOnUiThread(new Runnable() {
@Override
public void run() {
if (e instanceof GooglePlayServicesAvailabilityException) {
// The Google Play services APK is old, disabled, or not present.
// Show a dialog created by Google Play services that allows
// the user to update the APK
int statusCode = ((GooglePlayServicesAvailabilityException) e)
.getConnectionStatusCode();
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode,
MainActivity.this,
REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
dialog.show();
} else if (e instanceof UserRecoverableAuthException) {
// Unable to authenticate, such as when the user has not yet granted
// the app access to the account, but the user can fix this.
// Forward the user to an activity in Google Play services.
Intent intent = ((UserRecoverableAuthException) e).getIntent();
startActivityForResult(intent,
REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
}
}
});
}
override this method in the activity calling this Async task
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR)
&& resultCode == RESULT_OK) {
// Receiving a result that follows a GoogleAuthException, try auth again
// do the task you need to do after user grants permission
}
}
add this static variable to the activity
static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1001; // any number
来源:https://stackoverflow.com/questions/28170911/can-access-new-gmail-api-by-using-googleapiclientandroid