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

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

问题描述

因此,在更新之前,有一种简单的方法可以登录到谷歌驱动器和操纵你的谷歌文档 - 用红宝石。

之前,您可以用此登录到您的谷歌驱动器。

 需要'google_drive'
$会话= GoogleDrive.login(email@gmail.com,密码 )

但现在您收到警告讯息:

 警告:GoogleDrive.login已弃用,将在下一版本中删除。改为使用GoogleDrive.login_with_oauth。 

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

 要求'google / api_client'
要求'google_drive'

客户= Google: :APIClient.new(
:application_name =>'示例Ruby应用程序',
:application_version =>'1.0.0'

auth = client.authorization
auth.client_id =您的客户ID
auth.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。打开此页面:\ n%s \\\
\\\
%auth.authorization_uri)
print(2.输入页面显示的授权码:)
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并将它添加到脚本中。

 要求'google / api_client'
要求'google_drive'

客户= Google :: APIClient.new
access_token =TokenFromGoogleHardCoded

$ session = GoogleDrive.login_with_oauth(access_token)

#$ session does not connect

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

解决方案

我想到了这个烂摊子。

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

auth.scope是缺少一个网址。从github REFERENCE

  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。

 要求'google / api_client'
要求'google_drive'

客户= Google: :APIClient.new(
:application_name =>'示例Ruby应用程序',
:application_version =>'1.0.0'

auth = client.authorization
auth.client_id =您的客户ID
auth.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。打开此页面:\ n%s \\\
\\\
%auth.authorization_uri)
print(2.输入页面显示的授权码:)
auth.code = $ stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token

system'clear'
print保存您的访问令牌\\\
\\\

打印access_token
打印 \\\
保存您的刷新标记\\\
\\\

print auth.refresh_token

这段代码应该在控制台中输出你的access_token / refresh_token。
$ b

 需要google / api_client
需要google_driver
client = Google :: APIClient.new(
:application_name =>'示例Ruby应用程序',
:application_version =>'1.0.0'

auth = client .authorization
auth.client_id =您的客户ID
auth.client_secret =您的客户机密

access_token =您从终端保存的令牌

session = GoogleDrive.login_with_oauth(access_token)

用于session.files中的文件
p file.title
结束

最终您的access_token将过期。此时你需要刷新它。您可以通过调用

  auth.expires_at 
来查看您的access_token是否过期。

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

 需要'google / api_client'
需要'google_drive'


$ client_id =您的客户ID
$ client_secret =您的客户秘密
$ refresh_token =已保存的客户ID

客户= 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/
auth.redirect_uri =urn:ietf:wg:oauth:2.0:oob
auth.refresh_token = $ refresh_token

#这里是魔法酱
auth.refresh!

#你可以看到上面的auth.access_token没有被赋值
#但是如果你现在调用它,你会看到你有一个新的access_token

auth.access_token
=>来自服务器
的新标记


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.

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:\n%s\n\n" % 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)

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.

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

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.

解决方案

I figured out this mess.

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

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/"

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:\n%s\n\n" % 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\n\n"
print access_token  
print "\nSave your refresh token\n\n"
print auth.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

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天全站免登陆