即使从设备验证应用程序,也始终为NEED_PERMISSION获取UserRecoverableAuthException?

时间:2013-07-23 12:21:53

标签: android authentication oauth oauth-2.0

我正在使用Google Cross Client Identity。我遵循的程序是:

a)在Google API控制台中创建项目。 b)为已安装的应用程序(Android)生成Client-Id c)为Web应用程序生成Client-Id。 d)在Android端验证用户后,我必须在服务器端(离线)使用Google Drive用户。

为此,我创建了这样的范围:

String CLIENT_ID =“60000000007.apps.googleusercontent.com”; String scope =“server:client_id:”+ CLIENT_ID +“:api_scope:”+ DriveScopes.DRIVE +“”+“https://www.googleapis.com/auth/plus.login”;

现在我必须检索验证码,我必须转发到我的服务器端。 为此,这是我的完整源代码:

package com.example.googleaccess;

import java.io.IOException;

import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.plus.PlusClient;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.services.drive.DriveScopes;


public class MainActivity extends Activity {

    private String accountName = null;
    private GoogleAccountCredential credential;
    private int REQUEST_ACCOUNT_PICKER = 2;
    private int REQUEST_AUTHORIZATION = 11;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String CLIENT_ID = "60000000007.apps.googleusercontent.com";
        String scope = "server:client_id:"+CLIENT_ID+":api_scope:"+DriveScopes.DRIVE+" "+"https://www.googleapis.com/auth/plus.login";
        credential = GoogleAccountCredential.usingOAuth2(MainActivity.this, scope);
        startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
        GooglePlayServicesUtil.isGooglePlayServicesAvailable(MainActivity.this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return false;
    }

    class Async extends AsyncTask<Void, Void, Void> {

        Context credential = null;

        public Async(Context credential) {
            this.credential = credential;
        }

        @Override
        protected Void doInBackground(Void... params) {
            getAccessToken(credential);
            return null;
        }

    }

    public void getAccessToken(Context mContext) {

        try {

            String token = credential.getToken();
            Log.d("Token", "token:"+token);
        } catch (GooglePlayServicesAvailabilityException playEx) {
            playEx.getMessage();
            playEx.printStackTrace();

          }catch (UserRecoverableAuthException e) {
            e.printStackTrace();
            Log.d("Token", "token:"+e.getCause());

            startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("Token", "token:"+e.getMessage());
        } catch (GoogleAuthException e) {
            e.printStackTrace();
            Log.d("Token", "token:"+e.getMessage());
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_ACCOUNT_PICKER) {
            if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
            accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);

                if (accountName != null) {
                  credential.setSelectedAccountName(accountName);

                  new Async(getApplicationContext()).execute();
                }
              }
        }

        if(requestCode == REQUEST_AUTHORIZATION) {
            if (resultCode == Activity.RESULT_OK) {
                data.getExtras();
                new Async(getApplicationContext()).execute();
              } else {
                  startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
              }
        }
    }


}

这里我正在处理UserRecoverableAuthException,以便用户可以验证该应用程序。

当被要求获得访问我允许的数据的权限时,这段代码的一切正常 设备。但我再一次通过相同的异常让屏幕验证app为getToken()。 我不知道我缺少的地方。

如果我必须使用范围仅检索访问令牌,我的代码工作正常:

String scope = "oauth2:"+DriveScopes.DRIVE;

我还面临一个奇怪的问题,这个代码工作在模拟器4.2谷歌API 17版本上很好。但不是在任何设备上。

这是我的Logcat outfor这段代码:

07-23 18:03:36.992: W/GLSActivity(9639): [qq] Status from wire: NeedPermission status: NEED_PERMISSION
07-23 18:03:37.414: W/System.err(9626): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
07-23 18:03:37.414: W/System.err(9626):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
07-23 18:03:37.414: W/System.err(9626):     at com.example.googleaccess.MainActivity.getAccessToken(MainActivity.java:84)
07-23 18:03:37.414: W/System.err(9626):     at com.example.googleaccess.MainActivity$Async.doInBackground(MainActivity.java:67)
07-23 18:03:37.421: W/System.err(9626):     at com.example.googleaccess.MainActivity$Async.doInBackground(MainActivity.java:1)
07-23 18:03:37.421: W/System.err(9626):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-23 18:03:37.421: W/System.err(9626):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

1 个答案:

答案 0 :(得分:0)

此问题也在这里提出:

Android Google+ integration - repeated UserRecoverableAuthException

尚未找到任何解决方案,但调查仍在进行中。