如何在Python中获取OAuth访问令牌?

0 投票
3 回答
1798 浏览
提问于 2025-04-17 16:36

(我在superuser上问过这个问题,但没有得到回复...)

我正在尝试按照这个Dropbox API的教程进行操作,链接是http://taught-process.blogspot.com/2012/05/asdlasd-asda-sd-asd-asdasd.html

但是当我到达最后一部分时

#Print the token for future reference
print access_token

我得到的结果是

<dropbox.session.OAuthToken object at 0x1102d4210>

我该如何获得实际的令牌?它应该看起来像这样:

oauth_token_secret=xxxxxxx&oauth_token=yyyyyyy

(我在使用Mac)

3 个回答

0

我在这个教程中使用了Dropbox的API,链接是 http://taught-process.blogspot.com/2012/05/asdlasd-asda-sd-asd-asdasd.html

最后我写出了下面这个脚本,它对我来说是有效的。

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

# Get your app key and secret from the Dropbox developer website
APP_KEY = '3w7xv4d9lrkc7c3'
APP_SECRET = '1v5f80mztbd3m9t'

# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
ACCESS_TYPE = 'app_folder'

sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
request_token = sess.obtain_request_token()
url = sess.build_authorize_url(request_token)

# Make the user sign in and authorize this token
print "url:", url
print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
raw_input()

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

#Print the token for future reference
print access_token.key
print access_token.secret
1

你没错,确实是对的对象。但是你现在在处理的是一个类的实例。

<dropbox.session.OAuthToken object at 0x1102d4210>

这是Dropbox SDK为你创建的一个OAuthToken对象的实例。这个令牌似乎有两个属性:keysecret。这就是你的令牌密钥和秘密。这正是你需要的东西。

你可以这样来访问它们:

print access_token.key
print access_token.secret
1

看看这个对象的属性和方法,要做到这一点,可以对这个对象使用“dir”命令。

dir(access_token)

我很确定你会在这个对象里找到一些东西,可以给你需要的令牌。

撰写回答