2

我在https://console.developers.google.com/apis/credentials?project=XXXXX创建了一个项目和一个 Android 凭据

我正在按照以下步骤在我的 Android 应用上添加 Google 登录: https ://developers.google.com/identity/sign-in/android/start-integrating#prerequisites

我的第一个疑问是。在谷歌控制台上创建凭据后如何获取 google-services.json 文件?我只能下载 client_secret_XXXXXXXXXXX-asdfasdfasdfasdfasdfasdf.apps.googleusercontent.com.json

按照 Google 登录指南进行所有设置后,我无法从 Google API 获取令牌。我发现了这个问题:Android 中的新 Google 登录

第一步告诉您创建配置文件(它会为您创建一个 OAuth 2.0 客户端 ID 并将其插入到 google-services.json 中)

稍后,它再次说要创建 OAuth 2.0 客户端 ID,但这次它说您必须为 Web 应用程序执行此操作

这是令人困惑的部分!(至少对我来说)因为我只是获取为 android OAuth 创建的客户端 ID,而不是为 Web 应用程序创建一个新的客户端 ID(我认为文档只是多余的东西)

正如它所说,就是这个,只有这个是您必须用作方法 requestIdToken 或 requestServerAuthCode 的参数的一个。

忘记在此方法中使用 Android OAuth ID,因为那样您将一直收到丑陋的状态码响应 12501。

我认为主要问题是文档对此有点混乱。或者可能是因为您必须创建两个 OAuth ID 的事实有点奇怪。

总而言之,您需要两个 OAuth ID,一个用于 android,一个用于 Web 应用程序,您必须将每一个设置在正确的位置。

那里说我必须创建一个 Web 应用程序凭据。现在我很困惑,我有 2 个 json 文件(client_secret_XXXXXXXXXXX-asdfasdfasdfasdfasdfasdf.apps.googleusercontent.com.json 和 google-services.json,以及 2 个客户端 ID(Android 和 Web)

    // Configure sign-in to request the user's ID, email address, and basic
    // profile. ID and basic profile are included in DEFAULT_SIGN_IN.
    String serverClientId = getString(R.string.oauth_client_google);
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(serverClientId)
            .requestProfile()
            .build();
    // Build a GoogleApiClient with access to the Google Sign-In API and the
    // options specified by gso.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

R.string.oauth_client_google 是 Web 应用程序客户端 ID。如果我设置了 Android 客户端 ID,则无法正常工作。

我真的需要 Android 客户端 ID 吗?

任何人都知道正确的步骤:首先是谷歌帐户选择器,然后是来自谷歌的令牌?

4

1 回答 1

0

https://developers.google.com/identity/sign-in/android/start按照步骤配置和获取google_services.json。那里写的很清楚。不需要客户端密码。将该 google_services.json 文件保存在您的应用文件夹中。

然后在你的活动中声明

private GoogleApiClient mGoogleApiClient;

之后在 onCreate 方法中写入

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
              .requestEmail()
              .build();

      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
              .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
              .addApi(Plus.API)
              .build();

并继续按照谷歌文档https://developers.google.com/identity/sign-in/android/sign-in#add_the_google_sign-in_button_to_your_app

于 2016-03-29T17:22:58.827 回答