cron抓取工具使用Ruby中的Google API将数据插入Google电子表格中的授权问题 [英] Authorization issue with cron crawler inserting data into Google spreadsheet using Google API in Ruby
问题描述
我的项目是抓取某些网络数据,并将其放入我的Google电子表格中,每天早上9:00。它必须获得读取&写一些东西。这是为什么下面的代码位于顶部的原因。
My project is to crawl the certain web data and put them into my Google spreadsheet every morning 9:00. And it has to get the authorization to read & write something. That's why the code below is located at the top.
# Google API
CLIENT_ID = blah blah
CLIENT_SECRET = blah blah
OAUTH_SCOPE = blah blah
REDIRECT_URI = blah blah
# Authorization_code
def get_authorization_code
client = Google::APIClient.new
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI
uri = client.authorization.authorization_uri
Launchy.open(uri)
# Exchange authorization code for access token
$stdout.write "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!
client.authorization
end
authorization_code = get_authorization_code
access_token = authorization_code.access_token
session = GoogleDrive.login_with_oauth(access_token)
但是在代码中,即使我设置了cron作业,我可能需要在早上醒来, code>输入授权码即可完成。但我不想。
But with this in the code, even though I set up the cron job, I probably have to wake up in the morning and Enter authorization code
to get it done. But I don't want to.
请告诉我如何解决这个问题。
Please tell me how I can solve this problem.
推荐答案
感谢 Ruby google_drive gem oAuth2保存
我需要获取刷新令牌并让我的代码如下使用它。 p>
I needed to get a refresh token and make my code use it like below.
CLIENT_ID = '!!!'
CLIENT_SECRET = '!!!'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
REFRESH_TOKEN = '!!!'
client = Google::APIClient.new
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI
client.authorization.refresh_token = REFRESH_TOKEN
client.authorization.refresh!
access_token = client.authorization.access_token
session = GoogleDrive.login_with_oauth(access_token)
这篇关于cron抓取工具使用Ruby中的Google API将数据插入Google电子表格中的授权问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!