gpt4 book ai didi

python - 如何将 Python Keyring 与 Box API OAuth2 结合使用?

转载 作者:太空宇宙 更新时间:2023-11-04 04:53:41 31 4
gpt4 key购买 nike

我对 Python 和编程还很陌生,我正在尝试弄清楚如何使 box.com 身份验证过程自动化,这让我大吃一惊。任何帮助将不胜感激!

我在下面有这段代码,这显然不是我的,而是来自教程。我想弄清楚

keyring.get_password('Box_Auth', 'mybox@box.com')

我在想 mybox@box.com 是我的重定向 URI 吗?但是当它要求 Box_Auth 时,我不确定它在寻找什么。

完整代码如下

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

import keyring
from boxsdk import OAuth2
from boxsdk import Client

CLIENT_ID = ''
CLIENT_SECRET = ''


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)

再次强调,如果有任何帮助,我将不胜感激!

最佳答案

我遇到了完全相同的问题。

您将需要一个访问 token 和一个刷新 token 。阅读here如何生成这些。

关于python - 如何将 Python Keyring 与 Box API OAuth2 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47600448/

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