Skip to main content
 首页 » 编程设计

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

2025年01月19日10www_RR

我对 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如何生成这些。