为什么requestIdToken返回null? [英] why is requestIdToken returning null?

查看:154
本文介绍了为什么requestIdToken返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照此链接下面的步骤

  1. 我已将google-services.json下载到我的应用程序文件夹中.
  2. 我的项目级别gradle文件:

  1. I downloaded google-services.json to my app folder.
  2. My project level gradle file :

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'
    classpath 'com.google.gms:google-services:1.5.0-beta2'
}

  • 我的应用程序级别gradle文件:

  • My app level gradle file :

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.gms.google-services'
    
    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.google.android.gms:play-services-identity:8.3.0'
        compile 'com.google.android.gms:play-services:8.3.0'
        compile 'com.google.android.gms:play-services-plus:8.3.0'
        compile 'com.google.android.gms:play-services-auth:8.3.0'
        ...
    }
    

  • 我为后端服务器创建了OAuth 2.0客户端ID,并将该客户端ID传递给strings.xml文件.

  • I created OAuth 2.0 client ID for my backend server and pass this Client ID to strings.xml file.

    最后,我创建了GoogleSignInOptions和GoogleApiClient对象,如下所示:

    And finally I created GoogleSignInOptions and GoogleApiClient objects as below :

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestIdToken(getString(R.string.server_client_id))
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
    

    但是问题是result.isSuccess()在handleSignInResult函数中总是返回false.我在想,也许我在第一步或第二步中做错了什么.我的代码几乎与此

    But the problem is result.isSuccess() always returns false in handleSignInResult function. I'm thinking maybe I'm doing somethings wrongly in 1th or 2nd step. And my codes are almost similar to this SignInActivity.java. Any help would be appreciated.

    推荐答案

    正如我对以下问题的回答:

    As I have answered at the following question:

    Google Sign-in requestIdToken返回null

    为了成功请求ID令牌,您应该使用"Web应用程序"类型的客户端ID,而不是"Android"类型的客户端ID.

    In order to request Id Token sucessfully, you should use a "Web application" type Client Id, instead of "Android" type Client Id.

    您还可以在 Google文档中找到以下信息(请注意#3):

    You also find at Google's documentation, the following info (please note #3):

    为您的后端服务器创建OAuth 2.0客户端ID

    如果您的应用通过后端服务器进行身份验证或访问Google 来自后端服务器的API,您必须创建OAuth 2.0客户端ID 为您的服务器.要创建OAuth 2.0客户端ID,请执行以下操作:

    If your app authenticates with a backend server or accesses Google APIs from your backend server, you must create an OAuth 2.0 client ID for your server. To create an OAuth 2.0 client ID:

    1. 打开凭据"页面.
    2. 点击添加凭据> OAuth 2.0客户端ID.
    3. 选择 Web应用程序.
    4. 点击创建.
    1. Open the Credentials page.
    2. Click Add credentials > OAuth 2.0 client ID.
    3. Select Web application.
    4. Click Create.

    将此客户端ID传递到 requestIdToken 或 创建GoogleSignInOptions对象时 requestServerAuthCode 方法.

    Pass this client ID to the requestIdToken or requestServerAuthCode method when you create the GoogleSignInOptions object.


    更新3月26日:


    Update Mar 26:

    Android开发者博客-为以下项目注册OAuth客户端Google登录

    这篇关于为什么requestIdToken返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆