gpt4 book ai didi

python dropbox api - 保存 token 文件?

转载 作者:太空狗 更新时间:2023-10-29 21:16:53 25 4
gpt4 key购买 nike

我想避免不得不一遍又一遍地授权这个脚本。换句话说,当我从终端启动脚本时,它会给我一个链接,我必须在浏览器中打开它,然后单击浏览器中的“允许”按钮,然后返回到终端……我想有办法保存身份验证详细信息,但如何保存?

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website

APP_KEY = 'xxxxxxxxxxx'
APP_SECRET = 'yyyyyyyyyyyy'
ACCESS_TYPE = 'dropbox'


sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)

client = client.DropboxClient(sess)
#stored_creds = open(CONF_DIR + self.TOKEN_FILE).read()
print "linked account:", client.account_info()

f = open('t.txt')
response = client.put_file('/uploaded_with_python.txt', f)
print "uploaded:", response

folder_metadata = client.metadata('/')
print "metadata:", folder_metadata

f, metadata = client.get_file_and_metadata('/uploaded_with_python',rev='362e2029684fe')
out = open('/uploaded_with_python.txt', 'w')
out.write(f)
print(metadata)

-------------------------------------------- ------------------------------------------编辑

我修改了脚本并创建了脚本,但是我在读取 token 文件时仍然遇到问题

# Include the Dropbox SDK libraries
from dropbox import client, rest, session

# Get your app key and secret from the Dropbox developer website

APP_KEY = 'i4ffahjltei1bnu'
APP_SECRET = 'cjullao1iiymrse'
ACCESS_TYPE = 'dropbox'

#acces token file
token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )

request_token = sess.obtain_request_token()

# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
#save token file
TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS,'w')
token_file.write("%s|%s" % (access_token.key,access_token.secret) )
token_file.close()

client = client.DropboxClient(sess)

print "linked account:", client.account_info()

f = open('t.txt')
response = client.put_file('/uploaded_with_python.txt', f)
print "uploaded:", response

folder_metadata = client.metadata('/')
print "metadata:", folder_metadata

f, metadata = client.get_file_and_metadata('/uploaded_with_python',rev='362e2029684fe')
out = open('/uploaded_with_python.txt', 'w')
out.write(f)
print(metadata)

我收到这个错误:

Traceback (most recent call last):
File "dropb.py", line 14, in <module>
token_file = open(TOKENS)
NameError: name 'TOKENS' is not defined

最佳答案

您可以将 access_token 写入文件:

TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS,'w')
token_file.write("%s|%s" % (access_token.key,access_token.secret) )
token_file.close()

如果你这样做一次,那么之后你就可以使用那个 token :

token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()

sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE )
sess.set_token(token_key,token_secret)
client = client.DropboxClient(sess)

关于python dropbox api - 保存 token 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com