是一种使用GoogleApiClient从Android访问新gmail api并从帐户获取邮件的方法吗? 如果有可能,有人可以给我一个例子或指导我吗? 如果不是通过不使用imap或smtp从帐户获取消息的最佳方式是什么。
答案 0 :(得分:0)
我遇到了同样的问题,解决方案是在Google开发网站上提供的。这是如何做到的,将范围添加为" oauth2:" + GmailScopes.MAIL_GOOGLE_COM你可以添加多个范围逗号分隔但你必须在你输入的第一个范围之前使用oauth2:。这就是我称之为
的方式 new GetAuthTokenTask(MainActivity.this,
emailId,
"oauth2:" + GmailScopes.MAIL_GOOGLE_COM)
.execute();
这是Async任务的代码
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;
}
}
将此函数添加到执行此异步任务的活动
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
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
}
}
将此静态变量添加到活动
static final int REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR = 1001; // any number