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

Ruby google_drive gem oAuth2节省

发布时间:2020-12-17 01:50:48 所属栏目:百科 来源:网络整理
导读:所以在更新之前有一种简单的方法来登录谷歌驱动器并操纵你的谷歌文档 – 使用ruby. 在您使用此功能登录Google云端硬盘之前. require 'google_drive'$session = GoogleDrive.login("email@gmail.com","password") 但是现在你收到警告信息: WARNING: GoogleDr
所以在更新之前有一种简单的方法来登录谷歌驱动器并操纵你的谷歌文档 – 使用ruby.

在您使用此功能登录Google云端硬盘之前.

require 'google_drive'
$session = GoogleDrive.login("email@gmail.com","password")

但是现在你收到警告信息:

WARNING: GoogleDrive.login is deprecated and will be removed in the next version. Use GoogleDrive.login_with_oauth instead.

所以我去了git hub page,看看谷歌是如何让我们使用他们的高级语言服务,并发现他们希望我们使用oAuth2.像这样.

require 'google/api_client'
require 'google_drive'

client = Google::APIClient.new(
  :application_name => 'Example Ruby application',:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:n%snn" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token

$session = GoogleDrive.login_with_oauth(access_token)

这很好,除了我无法保存auth变量.
我想在脚本中对此进行硬编码,因此我不必继续谷歌来获取新的access_token.

所以我试图获取access_token并将其添加到脚本中.

require 'google/api_client'
require 'google_drive'

client = Google::APIClient.new
access_token = "TokenFromGoogleHardCoded"

$session = GoogleDrive.login_with_oauth(access_token)

# $session doesn't connect

我不确定我是否在正确的庄园里攻击这个问题.我想保存完整的auth变量并在我的脚本中硬编码.

解决方法

我想出了这个烂摊子.

auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"

auth.scope缺少一个url. REFERENCE来自github

auth.scope =
    "https://docs.google.com/feeds/" +
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"

您可以重复使用access_token,但首先需要获取它.警告:auth.access_token仅适用于一小时.如果你需要另一个,你需要调用auth.refresh!这将发给您另一个access_token.

require 'google/api_client'
require 'google_drive'

client = Google::APIClient.new(
  :application_name => 'Example Ruby application',:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
    "https://www.googleapis.com/auth/drive " +
    "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:n%snn" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token

system'clear'
print "Save your access tokennn"
print access_token  
print "nSave your refresh tokennn"
print auth.refresh_token

这段代码应该在您的控制台中打印出access_token / refresh_token.现在您可以对应用程序进行硬编码.

require "google/api_client"
require "google_driver"
client = Google::APIClient.new(
  :application_name => 'Example Ruby application',:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "Your Client ID"
auth.client_secret = "Your client secret"

access_token = "token you saved from the terminal"

session = GoogleDrive.login_with_oauth(access_token)

for file in session.files
   p file.title
end

最终你的“access_token”将过期.此时你需要“刷新”它.您可以通过调用来调用以查看您的access_token是否已过期

auth.expires_at

您可以使用此示例获取新的访问令牌.

require 'google/api_client'
require 'google_drive'


$client_id = "YOUR CLIENT ID"
$client_secret = "YOUR CLIENT SECRET"
$refresh_token = "SAVED REFRESH TOKEN"

 client = Google::APIClient.new(:application_name => 'Example Ruby application',:application_version => '1.0.0')
    auth = client.authorization
    auth.client_id = $client_id
    auth.client_secret = $client_secret
    auth.scope = "https://docs.google.com/feeds/" + "https://www.googleapis.com/auth/drive " + "https://spreadsheets.google.com/feeds/"
    auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
    auth.refresh_token = $refresh_token

    #here's the magic sauce 
    auth.refresh! 

    #as you can see above auth.access_token wasn't passed a value
    # but if you call it now you'll see you have a new access_token

    auth.access_token
    => new token from server

(编辑:李大同)

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

    推荐文章
      热点阅读