首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Google-plus:无法加载人员

Google-plus:无法加载人员
EN

Stack Overflow用户
提问于 2013-03-04 05:41:05
回答 2查看 6.8K关注 0票数 7

我想让谷歌加登录在我的应用程序。我可以连接,但无法获取当前用户的配置文件信息

代码语言:javascript
复制
@Override
public void onConnected() {

    String accountName = mPlusClient.getAccountName();

    mPlusClient.loadPerson(this, "me");
}

@Override
public void onPersonLoaded(ConnectionResult status, Person person) {

    Log.d("onPersonLoaded - ConnectionResult", "" + status);
    Log.d("onPersonLoaded - person", "" + person);
    if (status.getErrorCode() == ConnectionResult.SUCCESS) {
        Log.d(TAG, "Display Name: " + person.getDisplayName());
    }
}

我可以在onConnected中获得帐户名,但是onPersonLoaded给出了nullperson

日志显示:

代码语言:javascript
复制
onPersonLoaded - ConnectionResult(29861): ConnectionResult{statusCode=NETWORK_ERROR, resolution=null}
onPersonLoaded - person(29861): null

从docs 这里

公共静态最终int NETWORK_ERROR 发生网络错误。重试应该能解决问题。 常数:7 (0x00000007)

但无论我重试多少次,我都会犯同样的错误。这些是我使用的许可:

代码语言:javascript
复制
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />

我不知道如何解决这个问题,在过去的两个小时里我一直在寻找。任何帮助都是非常感谢的,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-14 13:21:21

最后我找到了答案。:)

loadPerson()loadPeople()只返回在导出的APK中用有效密钥签名的预期数据。在这里,我的意思是它应该与SHAI和您的API密钥控制台的其他规范相匹配。

在遵循样本代码时,您可能会忽略的另一件事是在PlusClient构造函数中定义作用域,如

代码语言:javascript
复制
mPlusClient = new PlusClient.Builder(this, this, this).setScopes(Scopes.PLUS_LOGIN)
                .setVisibleActivities("http://schemas.google.com/AddActivity").build();
票数 13
EN

Stack Overflow用户

发布于 2013-03-14 11:47:32

代码语言:javascript
复制
private class connectAsyncTask extends AsyncTask<String, Void, String> {

    String sAccessToken = null;

    @Override
    protected void onPostExecute(String info) {

        Log.d("JSONData = ", "" + info);
        //This contains all the data you wanted.
    }

    @Override
    protected String doInBackground(String... params) {

        HttpURLConnection urlConnection = null;
        try {
            URL url = new URL(
                    "https://www.googleapis.com/oauth2/v1/userinfo");
            sAccessToken = GoogleAuthUtil
                    .getToken(
                            MainActivity.this,
                            mPlusClient.getAccountName() + "",
                            "oauth2:"
                                    + Scopes.PLUS_PROFILE
                                    + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");

            //This is probably not the right way to do.
            //What I am doing here is, get an AccessToken, invalidate it and get a new
            //AccessToken again. Because I couldn't find a way to check whether the 
            //AccessToken is expired or not.

            GoogleAuthUtil.invalidateToken(MainActivity.this, sAccessToken);

            sAccessToken = GoogleAuthUtil
                    .getToken(
                            MainActivity.this,
                            mPlusClient.getAccountName() + "",
                            "oauth2:"
                                    + Scopes.PLUS_PROFILE
                                    + " https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email");

            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty("Authorization", "Bearer "
                    + sAccessToken);

            BufferedReader r = new BufferedReader(new InputStreamReader(
                    urlConnection.getInputStream(), "UTF-8"));
            StringBuilder total = new StringBuilder();
            String line;
            while ((line = r.readLine()) != null) {
                total.append(line);
            }
            line = total.toString();
            if (!TextUtils.isEmpty(line)) {
                return line;
            } else {
                return null;
            }
        } catch (UserRecoverableAuthException userAuthEx) {
            // Start the user recoverable action using the intent returned
            // by getIntent()

            userAuthEx.printStackTrace();
            mActivity.this.startActivityForResult(
                    userAuthEx.getIntent(), MY_ACTIVITYS_AUTH_REQUEST_CODE);
            return null;
        } catch (FileNotFoundException e) {
            //You get this exception when the AccessToken is expired.
            //I don't know how its a FileNotFoundException
            //Thats why instead of relying on this, I did the above to get
            //new AccessToken
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
    }

    @Override
    protected void onPreExecute() {
    }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15195264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档