问题描述
是通过使用来自帐户GoogleApiClient和获取消息从Android的新的Gmail API访问的方法吗?
如果有可能有人可以给我一个例子或指导我?
如果不是有什么不使用IMAP或SMTP来获得帐户的邮件的最佳途径。
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.
推荐答案
我面临同样的问题,解决的办法是在谷歌网站的发展给出。这是怎么了可以做到的,添加范围为的oauth2:+ GmailScopes.MAIL_GOOGLE_COM U可以添加逗号分隔的,但是你必须使用的oauth2 muliple范围:输入第一个范围之前。这是我称之为
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();
这就是code为异步任务
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
这篇关于可以通过使用GoogleApiClient(Android版)访问新的Gmail API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!