使用 Box.com SDK for Python

Working with the Box.com SDK for Python

我正在尝试开始使用 Box.com SDK,但我有几个问题。

from boxsdk import OAuth2

oauth = OAuth2(
    client_id='YOUR_CLIENT_ID',
    client_secret='YOUR_CLIENT_SECRET',
    store_tokens=your_store_tokens_callback_method,
)

auth_url, csrf_token = oauth.get_authorization_url('http://YOUR_REDIRECT_URL')

def store_tokens(access_token, refresh_token):
    # store the tokens at secure storage (e.g. Keychain)

1)什么是重定向 URL 以及如何使用它?我需要让服务器 运行 使用它吗?

2) store_tokens 方法中我需要什么样的代码?

我建议看一下 OAuth 2 tutorial。它将有助于更好地理解 OAuth 的工作原理以及各种参数的用途。

  1. 重定向 URL 设置在您的 Box application's settings:

    这是 URL Box 将发送可用于获取访问令牌的授权代码的位置。例如,如果您的重定向 URL 设置为 https://myhost.com,那么您的服务器将收到带有 URL 的请求,看起来类似于 https://myhost.com?code=123456abcdef.

    请注意,您的重定向 URI 不需要 是真实服务器。例如,使用 WebView 的应用有时会输入伪造的重定向 URL,然后直接从 WebView 中的 URL 中提取授权码。

  2. store_tokens 回调是可选的,但它可用于保存访问和刷新令牌,以防您的应用程序需要关闭。每次访问令牌和刷新令牌更改时都会调用它,让您有机会将它们保存在某个地方(到磁盘、数据库等)。

    您可以稍后将这些令牌传递给您的 OAuth2 构造函数,这样您的用户就不需要再次登录。

如果您只是进行测试,您还可以传入开发人员令牌。 This tutorial 解释如何。

这是对我有用的最基本的示例:

from boxsdk import Client, OAuth2

CLIENT_ID = ''
CLIENT_SECRET = ''
ACCESS_TOKEN = '' # this is the developer token

oauth2 = OAuth2(CLIENT_ID, CLIENT_SECRET, access_token=ACCESS_TOKEN)

client = Client(oauth2)

my = client.user(user_id='me').get()
print(my.name)
print(my.login)
print(my.avatar_url)
  1. 仅当您运行的 Web 应用程序需要响应用户的身份验证请求时才需要重定向 URL。如果您以编程方式进行身份验证,则只需将其设置为 http://localhost。在需要用户手动验证的情况下,重定向 URL 应该调用 Web 应用程序中的某些函数来存储和处理返回的验证码。您需要服务器 运行 吗?那么,如果您想对返回的身份验证代码做些什么,您指定的 URL 应该在您的控制之下并调用代码来做一些有用的事情。

  2. 下面是 store_tokens 函数的示例。它应该接受两个参数,access_tokenrefresh_token。在下面的示例中,该函数会将这些提交到本地存储,以便在 API 需要重新验证时使用:

来自here:

"""An example of Box authentication with external store"""

import keyring
from boxsdk import OAuth2
from boxsdk import Client

CLIENT_ID = 'specify your Box client_id here'
CLIENT_SECRET = 'specify your Box client_secret here'


def read_tokens():
    """Reads authorisation tokens from keyring"""
    # Use keyring to read the tokens
    auth_token = keyring.get_password('Box_Auth', 'mybox@box.com')
    refresh_token = keyring.get_password('Box_Refresh', 'mybox@box.com')
    return auth_token, refresh_token


def store_tokens(access_token, refresh_token):
    """Callback function when Box SDK refreshes tokens"""
    # Use keyring to store the tokens
    keyring.set_password('Box_Auth', 'mybox@box.com', access_token)
    keyring.set_password('Box_Refresh', 'mybox@box.com', refresh_token)


def main():
    """Authentication against Box Example"""

    # Retrieve tokens from secure store
    access_token, refresh_token = read_tokens()

    # Set up authorisation using the tokens we've retrieved
    oauth = OAuth2(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    access_token=access_token,
    refresh_token=refresh_token,
    store_tokens=store_tokens,
    )

    # Create the SDK client
    client = Client(oauth)
    # Get current user details and display
    current_user = client.user(user_id='me').get()
    print('Box User:', current_user.name)

if __name__ == '__main__':
    main()