Ruby google_drive gem oAuth2 保存 [英] Ruby google_drive gem oAuth2 saving

查看:12
本文介绍了Ruby google_drive gem oAuth2 保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在更新之前,有一种简单的方法可以登录 Google Drive 并使用 ruby​​ 操作您的 Google 文档.

So before the update there was a simple way to log into google drive and manipulate your google docs - with ruby.

在您能够使用此登录到您的谷歌驱动器之前.

Before you were able to log into your google drive with this.

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

但是现在您收到警告消息:

But now you get the warning message:

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

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

So I went to the git hub page to see how google wants us to use their services with high level languages, and found out they want us to use oAuth2. Like so.

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:
%s

" % 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.

This is fine and all but I can't save the auth variable. I would like to hard code this in the script so I don't have to keep going to google to get a new access_token.

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

So I've tried to get the access_token and adding it to the script like so.

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 变量并将其硬编码到我的脚本中.

I'm not sure I'm attacking this problem in the correct manor. I would like to save the complete auth variable and hard code it in my scripts.

推荐答案

我想出了这个烂摊子.

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

auth.scope 缺少 url.参考来自 github

The auth.scope is missing a url. REFERENCE from 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.

You can reuse your access_token, but first you need to get it. WARNING: The auth.access_token is only good for an hour. If you need another one you need to call auth.refresh! This will issue you another 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:
%s

" % 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 token

"
print access_token  
print "
Save your refresh token

"
print auth.refresh_token 

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

This chunk of code should print out your access_token/refresh_token in your console. Now you can hard code your application.

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"将过期.此时,您需要刷新"它.你可以通过调用

And eventually your "access_token" will expire. At this point you need to "refresh" it. You can call to see if your access_token is expired by calling

auth.expires_at

您可以通过此示例获取新的访问令牌.

You can get a new access token with this example.

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 

这篇关于Ruby google_drive gem oAuth2 保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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