【问题标题】:using devise+omniauth access_token to pull from Google Sheets使用 devise+omniauth access_token 从 Google 表格中提取
【发布时间】:2017-06-23 08:36:32
【问题描述】:

我想从已登录用户的 Google 表格电子表格中提取数据以进行某些处理。我在Google Console 中设置了一个应用程序:

  • Google 云端硬盘 API
  • 联系人 API
  • Google+ API
  • Google 表格 API

我还连接了devise/oauth,以便它可以成功登录。但是,每当我尝试使用在我的from_omniauth 方法中收到的access_token 时,我得到:

dailyLimitExceededUnreg: Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.

我做错了什么?

我的代码(使用google-api-client)如下:

# frozen_string_literal: true
require 'google/apis/drive_v2'

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:google_oauth2]
  has_many :datasets, inverse_of: :user

  # Omniauth support
  def self.from_omniauth(access_token)
    #session = GoogleDrive::Session.new( access_token )
    drive = Google::Apis::DriveV2::DriveService.new
    files = drive.list_files(
      fields: "nextPageToken, files(id, name)",
      q: "mimeType = 'application/vnd.google-apps.spreadsheet' AND trashed != true",
    )


    data = access_token.info
    user = User.where(email: data['email']).first

    unless user
      user = User.create(
        email: data['email'],
        password: Devise.friendly_token[0, 20]
      )
    end
    user
  end
end

为了记录,我也尝试使用google-drive gem 并遇到了相同的响应。

【问题讨论】:

    标签: devise google-drive-api ruby-on-rails-5 omniauth google-sheets-api


    【解决方案1】:

    该错误消息意味着您对云端硬盘的请求缺少 Authorization: http 标头。这通常看起来像Authorization: bearer xxxxxxxxx,其中 xxxxx 是访问令牌。

    我在您的代码中看不到您将访问令牌设置为 drive 对象的任何地方(但请注意,我不知道 Ruby)。

    【讨论】:

      【解决方案2】:

      虽然@pinyyid 本身没有完整的答案,但它确认我没有正确传递凭据(我之前尝试过,但没有正确)。最终,需要进行以下更改才能使其使用 google-drive gem 工作:

      config.omniauth :google_oauth2, '<CLIENT_ID>', '<SECRET_KEY', scope: 'https://www.googleapis.com/auth/drive,https://spreadsheets.google.com/feeds/,https://www.googleapis.com/auth/plus.login,https://www.googleapis.com/auth/userinfo.email,https://www.googleapis.com/auth/userinfo.profile'
      

      这里的关键是将我需要的范围添加到设计请求中。

      然后,在我的用户对象中,我使用了以下内容:

      def self.from_omniauth(access_token)
         session = GoogleDrive::Session.from_access_token(access_token[:credentials][:token])
         x = session.spreadsheet_by_title '<a spreadsheet title>' #or any call, really
      

      我希望这对某人有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 1970-01-01
        相关资源
        最近更新 更多