我正在尝试创建一个python脚本,它以一个(带有访问令牌和文件的.csv)作为输入,并将该文件上传到多个谷歌驱动器,这些驱动器的访问令牌都在该csv中,但有时访问令牌过期了,我必须让它们again...just看到有一种叫做刷新的东西,它刷新访问令牌。
是否可以通过python脚本完成此操作,请解释。刷新令牌过期吗?
import json
import requests
import pandas as pd
headers = {}
para = {
"name": "update",
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open("./update.txt", "rb")
}
tokens = pd.read_csv('tokens.csv')
for i in tokens.token:
headers={"Authorization": i}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print(r.text)
发布于 2021-06-08 23:48:12
为了能够获得refresh_token
,必须将access_type
设置为当将用户重定向到谷歌的OAuth 2.0服务器时的offline
。
如果这样做,如果您对access_token
执行以下POST
请求,则可以得到一个新的https://oauth2.googleapis.com/token
POST /token HTTP/1.1
Host: oauth2.googleapis.com
Content-Type: application/x-www-form-urlencoded
client_id=your_client_id&
client_secret=your_client_secret&
refresh_token=refresh_token&
grant_type=refresh_token
相应的回应如下:
{
"access_token": "1/fFAGRNJru1FTz70BzhT3Zg",
"expires_in": 3920,
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}
注意:
您可以在下面提供的参考中找到几种语言的代码片段,包括Python,但考虑到您没有使用Python库,我认为我提供的HTTP/REST代码段在您的情况下可能更有用。
参考资料:
https://stackoverflow.com/questions/67893517
复制相似问题