【问题标题】:Can access new gmail api by using GoogleApiClient(Android)?可以使用 GoogleApiClient(Android) 访问新的 gmail api 吗?
【发布时间】:2016-01-12 20:17:48
【问题描述】:

是否可以通过使用 GoogleApiClient 从 android 访问新的 gmail api 并从帐户获取消息? 如果可能的话,有人可以给我一个例子或指导我吗? 如果不是,不使用 imap 或 smtp 从帐户获取消息的最佳方法是什么。

【问题讨论】:

  • 根据链接,json 文件应作为输入。我们如何在android中提供它,它不安全。不是吗?
  • 我无法让 GoogleApiClient 工作并开始使用正在工作的 SMTP。只是想知道您为什么不想使用 IMAP 或 SMTP?
  • 我不想使用 IMAP,因为我在新 gmail api 的介绍中看到它的性能更高,我想了解一下。
  • 我认为一个好的开始是从 Android 的任务示例应用程序开始。不是吗?

标签: android gmail-api


【解决方案1】:

我遇到了同样的问题,解决方案是在谷歌开发网站上给出的。这就是它的完成方式,将范围添加为“oauth2:”+ GmailScopes.MAIL_GOOGLE_COM 您可以添加多个范围逗号分隔,但您必须在输入第一个范围之前使用 oauth2:。我是这样称呼它的

 new GetAuthTokenTask(MainActivity.this,
            emailId,
            "oauth2:" + GmailScopes.MAIL_GOOGLE_COM)
            .execute();

这是异步任务的代码

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

【讨论】:

  • 我认为这个答案没有回答所提出的问题。目前,似乎根本无法通过将正确的配置添加到您的 GoogleApiClient.Builder#addApi(...) 函数来使用 Gmail API。 this (unanswered) question 的存在支持这一观察,而且我还没有发现任何相反的证据 - 无论是在这个网站上,还是在 Google 开发者文档上。
猜你喜欢
  • 2014-10-29
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-21
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多