首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何处理google_drive gem中的grant_type错误?

如何处理google_drive gem中的grant_type错误?
EN

Stack Overflow用户
提问于 2016-06-30 07:02:09
回答 1查看 725关注 0票数 2

我正在尝试使用Ruby gem 'google_drive‘。在使用这个gem之前,我将通过gem omniauth-google-oauth2获取用户的令牌。

当我尝试使用google_drive时,如下所示:

代码语言:javascript
复制
  def google_oauth2(current_user)

    session = GoogleDrive.login_with_oauth(self.token)

    # Gets list of remote files.
    session.files.each do |file|
      p file.title
    end
  end 

我得到以下错误:

代码语言:javascript
复制
Sending HTTP get https://www.googleapis.com/drive/v3/files?fields=%2A
Caught error Authorization failed.  Server message:
{
 "error": "invalid_request",
 "error_description": "Required parameter is missing: grant_type"
}
Error - #<Signet::AuthorizationError: Authorization failed.  Server message:
{
 "error": "invalid_request",
 "error_description": "Required parameter is missing: grant_type"
}>

Completed 500 Internal Server Error in 128ms (ActiveRecord: 0.4ms)

我该如何解决这个问题?

更新

用于存储google oauth 2令牌的Omniauth代码:

代码语言:javascript
复制
  def google_oauth2
    auth_hash = request.env['omniauth.auth']

    @authentication = Authentication.find_or_create_by(
                                            user_id: current_user.id,
                                            provider: auth_hash["provider"],
                                            uid: auth_hash["uid"]
                                          )
    @authentication.update_attributes(
                                      :token => auth_hash['credentials']['token'],
                                      :refresh_token => auth_hash['credentials']['refresh_token'],
                                      :provider_description => auth_hash["info"].email
                                    )

    flash[:notice] = "google_oauth2 authed."
    redirect_to '/'
  end
EN

回答 1

Stack Overflow用户

发布于 2016-06-30 23:20:39

无需config.json即可访问google drive api的工作解决方案。此解决方案使用从google auth 2 omniauth策略获得的刷新令牌:

代码语言:javascript
复制
  require 'google/apis/drive_v2'
    auth = Signet::OAuth2::Client.new(
      token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
      client_id:            "XXX-XXX",
      client_secret:        "XXX",
      refresh_token:        self.refresh_token # Get this from the omniauth strategy. 
    )

    auth.fetch_access_token!

    x = Google::Apis::DriveV2
    drive = x::DriveService.new
    drive.authorization = auth

files = drive.list_files

这花了我.5一天的时间。我希望它能帮助其他人!:)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38111436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档