加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何在Ruby on Rails服务器上验证从Android发

发布时间:2020-12-17 02:09:56 所属栏目:百科 来源:网络整理
导读:我有一个 Android登录的 Android应用程序.根据 documentation,我生成了一个令牌ID: // Configure Google Sign-In with the requestIdTokenGoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN
我有一个 Android登录的 Android应用程序.根据 documentation,我生成了一个令牌ID:

// Configure Google Sign-In with the requestIdToken

GoogleSignInOptions googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.server_client_id))
                .requestEmail()
                .build();

// Handle result

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount account = result.getSignInAccount();
        String tokenId = account.getIdToken();
    }
}

我在服务器端遇到了问题,使用Ruby on Rails.我正在尝试使用google-id-token宝石. README给出了以下示例:

validator = GoogleIDToken::Validator.new(expiry: 1800)
begin
  payload = validator.check(token,required_audience,required_client_id)
  email = payload['email']
rescue GoogleIDToken::ValidationError => e
  report "Cannot validate: #{e}"
end

我有令牌(来自android java代码).什么是required_audience?我应该使用我的客户端应用程序的相同client_id吗?当我尝试在服务器上运行代码时,我的有效负载为零.

此外,我想知道这是否是验证令牌ID的正确方法.

解决方法

经过一些研究,我找到了自己问题的答案.他们是:

什么是required_audience?

它可以从解码的JWT字符串中获得.您可以按如下方式解码它:

JWT.decode(token,nil,false)

我应该使用我的客户端应用程序的相同client_id吗?

是. required_audience和required_client_id应该相同.否则,验证失败

那为什么我的有效载荷为零?

问题是,gem in GitHub和RubyGems中的一个是不同的.我通过将Gemfile gem指向GitHub解决了这个问题:

gem 'google-id-token',git: 'https://github.com/google/google-id-token.git'

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读