【问题标题】:How can I access the Google account user_id?如何访问 Google 帐户 user_id?
【发布时间】:2012-09-05 22:32:04
【问题描述】:

我想为authentication 访问用户的Google 帐户user_id,但我在AccountManager 中没有看到任何提及。

我的应用如何请求user_id

【问题讨论】:

    标签: android oauth openid accountmanager


    【解决方案1】:

    现在Google Play Services 可用,您可以使用它来请求用户访问https://www.googleapis.com/auth/userinfo.profile 范围的权限,并使用生成的访问令牌向https://www.googleapis.com/oauth2/v1/userinfo?access_token={accessToken} 发出请求以获取他们的用户ID。

    【讨论】:

    • userinfo?access_token={accessToken} 请求是否记录在某处?
    • I noticed Note: Google Play services has not launched, but we want to give you a quick overview of what to expect. 这是否意味着 API 尚不可用,或者它处于某种测试状态?
    • @JeffAxelrod 这是正确的。 API 的文档现在可用,仅此而已。现在,使用 Android AccountManager 的 getAuthToken API。这是一个例子:stackoverflow.com/a/10988589/313790
    • @JeffAxelrod 仅供参考:Google Play 服务现在可供开发人员使用。您需要查看 GoogleAuthUtil 和 AccountPicker。
    【解决方案2】:

    在 Google Play 服务发布之前,您需要使用 Android AccountManager 的 getAuthToken API。

    这是一个如何使用 getAuthToken 获取access_token 的示例:https://stackoverflow.com/a/10988589/313790

    【讨论】:

      【解决方案3】:

      查看 Google 的 AccountManager example for the Tasks API

      获得访问令牌后,您就可以使用 google-api-java-client 的 oauth2 library 来请求 Userinfo 对象,而不是使用任务库,就像在 this example 中一样:

      Oauth2 oauth2 = new Oauth2.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
         .setApplicationName("Google-OAuth2Sample/1.0").build();
      Userinfo userinfo = oauth2.userinfo().get().execute();
      String userId = userinfo.getId();
      

      【讨论】:

        猜你喜欢
        • 2012-05-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-06
        • 2020-04-13
        • 2019-02-28
        • 2012-03-15
        • 2015-04-29
        相关资源
        最近更新 更多