【问题标题】:message NeedPermission when get token code获取令牌代码时的消息 NeedPermission
【发布时间】:2014-03-13 14:56:28
【问题描述】:

我使用下面的sn-p来获取token:

private class task extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        Bundle appActivities = new Bundle();
        appActivities.putString(
                GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES,
                Constants.ADD_ACTIVITY_SCHEME + " "
                        + Constants.BUY_ACTIVITY_SCHEME);

        String serverClientID = "My_Client_Id";
        String scopes = "oauth2:server:client_id:" + serverClientID
                + ":api_scope:" + Scopes.PLUS_LOGIN + " "
                + Scopes.PLUS_PROFILE;
        String code = null;
        try {

            code = GoogleAuthUtil.getToken(MainActivity.this, // Context
                                                                // context
                    mPlusClient.getAccountName(), // String accountName
                    scopes, // String scope
                    appActivities // Bundle bundle
                    );

        } catch (IOException transientEx) {
            code = "Loi 1";
        } catch (UserRecoverableAuthException e) {
            code = "Loi 2: "+e.getMessage();
        } catch (GoogleAuthException authEx) {
            code = "Loi 3";
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return code;
    }

    @Override
    protected void onPostExecute(String token) {
        showToast(token);
    }
}

我在 onConnected 方法中执行这行代码:

new task.execute();

UserRecoverableAuthException 发生并且我的 toast 显示消息:“NeedPermission”。

我该如何解决?

【问题讨论】:

  • 我确实遵循@Rrthinavel 代码,我收到了这样的 id_token:ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI...我将此 id_token 传递到 google url lis=ke this:googleapis.com/oauth2/v1/…...我收到一条消息: {"error": "invalid_token","error_description": "Invalid Value"} 我认为 getToken() 3 args 和 getToken() 4 args 之间可能存在差异。当我跟随你编码时,我得到令牌代码,但验证时无效。我通过上面的代码尝试代码,我得到一个例外:NeedPermission。请帮我!谢谢。
  • 任何帮助。我需要解决这个问题。谢谢。
  • @Nguyen 试试这个网址:graph.facebook.com/me?access_token="Your 访问令牌"

标签: android oauth


【解决方案1】:

您是否在清单中添加了以下权限?

<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

这是我的工作代码!

    @Override
public void onConnected(Bundle arg0) {
    mSignInClicked = false;
    Toast.makeText(this, "User is connected!", Toast.LENGTH_LONG).show();
    String accountName = mPlusClient.getAccountName();
    // Get user's information

    task = new AsyncTask<Void, Void, String>() {
        @Override
        protected String doInBackground(Void... params) {
            String token = null;

            try {
                token = GoogleAuthUtil.getToken(MyNetwork.this,
                        mPlusClient.getAccountName(), "oauth2:"
                                + Scopes.PROFILE);
                Log.i("TAG", "token" + token);
            } catch (IOException transientEx) {
                // Network or server error, try later
                Log.e(TAG, transientEx.toString());
            } catch (UserRecoverableAuthException e) {
                // Recover (with e.getIntent())
                Log.e(TAG, e.toString());
                Intent recover = e.getIntent();
                startActivityForResult(recover, REQUEST_CODE_TOKEN_AUTH);
            } catch (GoogleAuthException authEx) {

                Log.e(TAG, authEx.toString());
            }
            return token;
        }

        @Override
        protected void onPostExecute(String token) {
            Log.i(TAG, "Access token retrieved:" + token);
            mHandler.sendEmptyMessage(STOP_PROGRESS);
            getProfileInformation(token);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mHandler.sendEmptyMessage(SHOW_PROGRESS);
        }

    };
    task.execute();
    Toast.makeText(this, accountName + " is connected.", Toast.LENGTH_LONG)
            .show();

}

我正在获取如下用户信息。

/**
 * Fetching user's information name, email, profile pic
 * */
private void getProfileInformation(String mToken) {

    String mAccessToken = mToken == null ? "" : mToken;
    String mProfileId = "";
    String mProfileName = "";
    String mImageUrl = "";
    String mSecretKey = "";

    try {
        if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {

            Person currentPerson = Plus.PeopleApi
                    .getCurrentPerson(mGoogleApiClient);

            txtGooglePlus.setText(currentPerson.getDisplayName());
            mProfileName = currentPerson.getDisplayName();
            mImageUrl = currentPerson.getImage().getUrl();
            String personGooglePlusProfile = currentPerson.getUrl();
            String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
            Log.e(TAG, "Name: " + mProfileName + ", plusProfile: "
                    + personGooglePlusProfile + ", email: " + email
                    + ", Image: " + mImageUrl);
            mProfileId = currentPerson.getId();


        } else {
            Toast.makeText(getApplicationContext(),
                    "Person information is null", Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

它对我来说很好用。经测试。检查并随时询问是否有任何问题。

【讨论】:

  • 我在 AndroidManifest.xml 中都需要权限,就像这里一样。 ' '
  • 请告诉我什么是 REQUEST_CODE_TOKEN_AUTH 以及它的价值。谢谢!
  • 是任意整数值来标识请求。在我的情况下,它是。 private static final int REQUEST_CODE_TOKEN_AUTH = 100;
  • 在您的“范围”和以下范围之间确实存在差异: String scopes = "oauth2:server:client_id::api_scope:";
  • @Rrthinavel:我确实遵循您的代码,我收到了这样的 id_token:ya29.1.AADtN_Wtc1xlFEILNL9aUdp98dE6yI...我将此 id_token 传递到 google url lis=ke this:googleapis.com/oauth2/v1/…...我收到一条消息: {"error": "invalid_token","error_description": "Invalid Value"} 我认为 getToken() 3 args 和 getToken() 4 args 之间可能存在差异。当我跟随你编码时,我得到令牌代码,但验证时无效。我通过上面的代码尝试代码,我得到一个例外:NeedPermission。请帮我!谢谢。
猜你喜欢
  • 2016-05-20
  • 2021-08-19
  • 2019-09-10
  • 1970-01-01
  • 2014-11-26
  • 2015-12-05
  • 1970-01-01
  • 2016-09-04
  • 1970-01-01
相关资源
最近更新 更多