【问题标题】:Get User Name from Google Plus in android在 android 中从 Google Plus 获取用户名
【发布时间】:2023-03-25 06:58:01
【问题描述】:

我已将 Google plus 与我的 Android 应用程序集成。一切正常,我也连接到 Google plus,但我无法获取当前用户的登录名称。

public void onConnected(Bundle connectionHint) {
    String personName="Unknown";
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        personName = currentPerson.getDisplayName();
    }
}

Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)这个方法总是返回null

任何帮助将不胜感激。 谢谢。

【问题讨论】:

  • String personGooglePlusProfile = currentPerson.getUrl();添加这一行
  • 您是否在if (Plus....) 语句中缺少}
  • } 在此处粘贴代码时缺少此括号。
  • @Anjali,感谢您的回复。当我将 currentPerson 设为 null 时,我无法调用 getUrl。它会给出空指针异常。

标签: android google-api google-plus


【解决方案1】:

对我来说,这个调用返回 null 的原因是我的应用程序没有启用 Google+ API。导航到https://console.developers.google.com,选择您的项目并启用 Google+ API 以使其正常工作!

【讨论】:

  • 我不敢相信我错过了这么简单的事情,我启用了 google + domain api。
  • 我遇到了同样的问题。我也不敢相信我错过了首次启用 Google Plus api 这样一个简单的概念。
  • 我启用了此功能并执行了上述步骤,但它仍然不适合我? :(
【解决方案2】:

你必须添加这一行:

Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

像这样:

public void onConnected(Bundle connectionHint) {

    /* This Line is the key */
    Plus.PeopleApi.loadVisible(mGoogleApiClient, null).setResultCallback(this);

    String personName="Unknown";
    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
       Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
       personName = currentPerson.getDisplayName();
       .........
    }
}

【讨论】:

  • 谢谢,免去执行。但是为什么在地球上没有人提到这很重要:(,再次感谢您
  • 整天都在寻找这个,甚至谷歌提供的用于 G+ 登录的 QuickStart 示例在 onConnected 处也有 NullPointerException。好可惜?
  • 我在片段中使用 onconnected 和 setResultCallback(this);无法解决“这个”。我该如何解决这个问题?
  • @deepak 可能为时已晚,但一种方法是做到这一点: setResultActivityCallback(getActivity());如果您确实需要在片段内而不是在托管活动中处理回调,您可以在活动中为您的片段调用 onActivityResult
【解决方案3】:

在我的情况下,返回 null 是因为我添加了该行:

addScope(new Scope(Scopes.EMAIL))

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(new Scope(Scopes.PROFILE))
            //.addScope(new Scope(Scopes.EMAIL))
            .build();

================== 更新======================== 范围应设置如下:

mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API)
            .addScope(Plus.SCOPE_PLUS_PROFILE)
            .addScope(Plus.SCOPE_PLUS_LOGIN)
            .build();

【讨论】:

  • 除了 aove 解决方案之外,您需要确保的是,使用 OAuth 在 Google API 控制台中添加凭据。
【解决方案4】:

检查您是否通过 SHA1 密钥库生成了调试和生产客户端 ID。 它显示在您的projectAPIs & Auth -> Credentials 选项卡中。

Setting up OAuth 2.0 for Android 文档说:

在终端中,运行 Keytool 实用程序以获取数字签名 .apk 文件的公共证书的 SHA1 指纹。

...调试密钥库的示例...

重要提示:当您准备向用户发布应用时,请再次执行这些步骤并为您的生产应用创建一个新的 OAuth 2.0 客户端 ID。对于生产应用,请使用您自己的私钥对生产应用的 .apk 文件进行签名。

【讨论】:

    【解决方案5】:

    我尝试了很多方法,但仍然面临同样的问题。最后,在 app build.gradle 文件中,我发现我的 applicationId 与 Credentials 中的包名不一样。我解决了这个问题,一切都很顺利。

    因此,请确保:

    • build.gradle 中的 applicationId
    • AndroidManifest.xml 中的包名
    • 凭据中的包名称

    都是一样的。

    希望这对某人有所帮助。

    【讨论】:

    • 已解决,在我的情况下 applicationIdManifest package name 不同。
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多