5

我正在尝试开始使用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 方法中需要什么样的代码?

4

3 回答 3

12
  1. 仅当您运行需要响应用户请求以进行身份​​验证的 Web 应用程序时,才需要重定向 URL。如果您以编程方式进行身份验证,您可以简单地将其设置为http://localhost。在需要用户手动进行身份验证的场景中,重定向 URL 应该调用 Web 应用程序中的某个函数来存储和处理返回的身份验证代码。您需要运行服务器吗?好吧,如果您想对返回的身份验证代码做一些事情,您指定的 URL 应该在您的控制之下并调用代码来做一些有用的事情。

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

这里开始:

"""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()
于 2016-07-01T14:26:48.867 回答
3

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

  1. 重定向 URL 在您的Box 应用程序的设置中设置

    Box 应用程序设置的屏幕截图

    这是 Box 将发送可用于获取访问令牌的身份验证代码的 URL。例如,如果您的重定向 URL 设置为https://myhost.com,那么您的服务器将收到一个 URL 类似于 的请求https://myhost.com?code=123456abcdef

    请注意,您的重定向 URI 不需要真实服务器。例如,使用 WebView 的应用有时会输入一个虚假的重定向 URL,然后直接从 WebView 中的 URL 中提取身份验证代码。

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

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

于 2015-04-13T07:34:27.227 回答
0

如果你只是在测试,你也可以传入一个开发者令牌。本教程解释了如何。

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

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)
于 2016-03-03T06:58:51.357 回答