Android - 使用服务器访问gmail的范围

时间:2014-07-31 15:57:28

标签: android gmail imap

我想与oAuth2联系谷歌的IMAP。但该应用程序似乎并未真正授权。

我尝试使用范围oauth2:https://mail.google.com/,但服务器无法使用返回的令牌。

然后我想在Android范围内设置更多数据:“INVALID_SCOPE”。

范围是:

String scope = String.format("oauth2:server:%s:client_id:%s:api_scope:%s",
                             API_KEY, CLIENT_ID, "https://mail.google.com/");

使用:
API KEY:服务器应用程序的API KEY(不是Web应用程序客户端Api密钥)
CLIENT_ID:服务帐户(不是Web应用程序)的客户端ID

完整的代码是:

public static String getToken(final Context context, final Activity activity, final String accountName)
{
    AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>()
    {
        @Override
        protected String doInBackground(Void... params)
        {
            String token = null;

            try
            {
                String scope = String.format("oauth2:server:%s:client_id:%s:api_scope:%s", API_KEY, CLIENT_ID, "https://mail.google.com/");
                Log.i("GooglePlusSignupTool", "scope : "+scope);
                token = GoogleAuthUtil.getToken(context, accountName, scope);
            }
            catch (IOException transientEx)
            {
                // Network or server error, try later
                Log.e("GoogleSignupTool", transientEx.toString());
            }
            catch (UserRecoverableAuthException e)
            {
                // Recover (with e.getIntent())
                Log.e("GoogleSignupTool", e.toString());
                Intent recover = e.getIntent();
                context.startActivity(recover);//ForResult(recover, REQUEST_CODE_TOKEN_AUTH);
            }
            catch (GoogleAuthException authEx)
            {
                // The call is not ever expected to succeed
                // assuming you have already verified that
                // Google Play services is installed.
                Log.e("GoogleSignupTool", authEx.toString());
            }

            return token;
        }

        @Override
        protected void onPostExecute(String token)
        {
            Log.i("GoogleSignupTool", "Access token retrieved:" + token);
        }

    };
    task.execute();
    Log.i("GoogleSignupTool", "END");
    return "";
}

0 个答案:

没有答案