如何在Django Fram中使用BoxAPI

2024-04-20 02:46:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Django框架创建一个web应用程序(我是web应用程序创建和Django的初学者),并希望使用Box api访问媒体/mp3文件。我需要使用OAuth2进行身份验证,并且我尝试遵循本页(https://developer.box.com/docs/authenticate-with-oauth-2)上的说明,但是我不确定代码块应该放在哪里。例如,我假设

# Auth config
client_id = 'YOUR CLIENT ID'
client_secret='YOUR CLIENT SECRET'
redirect_uri = 'http://127.0.0.1:5000/return'

会破产的设置.py, 但是我不确定其他的代码块,比如

from boxsdk import Client
from boxsdk import OAuth2

import config_oauth

# Create new OAuth client & csrf token
oauth = OAuth2(
  client_id=config_oauth.client_id,
  client_secret=config_oauth.client_secret
)
csrf_token = ''

或者

global csrf_token
auth_url, csrf_token = oauth.get_authorization_url(config_oauth.redirect_uri)

return redirect(auth_url)

以及

# Fetch access token and make authenticated request
@app.route('/return')
def capture():
  # Capture auth code and csrf token via state
  code = request.args.get('code')
  state = request.args.get('state')

  # If csrf token matches, fetch tokens
  assert state == csrf_token
  access_token, refresh_token = oauth.authenticate(code)

  # PERFORM API ACTIONS WITH ACCESS TOKEN

我想这些应该放在下面视图.py但是当我这样做的时候,我得到了一些语法错误。任何帮助都将不胜感激!你知道吗


Tags: importclienttokenauthidconfigurlsecret