【问题标题】:Ruby google_drive gem oAuth2 savingRuby google_drive gem oAuth2 保存
【发布时间】:2015-07-24 19:53:04
【问题描述】:

所以在更新之前,有一种简单的方法可以登录到 google drive 并操作您的 google 文档 - 使用 ruby​​。

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

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 看看 google 希望我们如何使用高级语言的服务,并发现他们希望我们使用 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%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)

这很好,但我无法保存 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 变量并将其硬编码到我的脚本中。

【问题讨论】:

    标签: ruby oauth google-drive-api


    【解决方案1】:

    我发现了这个烂摊子。

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

    这段代码应该在你的控制台中打印出你的 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 
    

    【讨论】:

      【解决方案2】:

      您可能会发现使用服务帐户更容易,如下所述:

      https://github.com/gimite/google-drive-ruby/issues/126#issuecomment-73542381

      【讨论】:

        【解决方案3】:

        虽然 Duck1337 的回答会起作用,但您实际上并不需要使用 google_drive 1.0.* 调用 auth.refresh!

        您需要做的就是在第一次提供授权码时保存 refresh_token,然后始终在调用 auth.fetch_access_token! 之前传递该授权码!令牌会自动刷新。

        下面是我用来登录的。我使用环境变量而不是代码常量,因此我可以将它们添加到 Heroku Config Vars 中,并且还可以将敏感数据排除在代码之外。

        client = Google::APIClient.new
            auth = client.authorization
            auth.client_id = ENV['GOOGLE_CLIENT_ID']
            auth.client_secret = ENV['GOOGLE_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"
        
            if ENV['GOOGLE_REFRESH_TOKEN'] == "0" # Always start with 0 in your .env file, e.g. GOOGLE_REFRESH_TOKEN = 0
              puts("1. Open this page:\n%s\n\n" % auth.authorization_uri)
              puts("2. Enter the authorization code shown in the page: ")
              auth.code = $stdin.gets.chomp
            end if
        
            auth.refresh_token = ENV['GOOGLE_REFRESH_TOKEN'] #this will be 0 the first time around, but that's OK
            auth.fetch_access_token!
        
            if ENV['GOOGLE_REFRESH_TOKEN'] == "0"   
              puts("3. Save this refresh token:\n%s\n\n" % auth.refresh_token) # you can now replace the 0 in your .env file with this token, so it'll be used permanently.  You can also add this to the Heroku Config Vars.
              ENV['GOOGLE_REFRESH_TOKEN'] = auth.refresh_token #we'll set it here, so it works without having to alter the .env file for this call
            end if
        
            GoogleDrive.login_with_oauth(auth.access_token)
        

        【讨论】:

          【解决方案4】:

          使用 capybara 并导航到令牌页面。从页面源中获取它并保存到变量中。需要时插入变量。令牌字符串已过期,因此我每次需要访问电子表格时都会获取一个新字符串。

          【讨论】:

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