您不应在查询字符串中传递访问令牌,例如/?token=my_token。这不是一种安全的方式,绝对不推荐。
您可以使用的其他一些方法是:
方法 1:在响应标头中设置 server_access_token
您可以在响应标头中设置访问令牌并使用 HTTPS 协议发送。
令牌将被发送一次并由客户端使用。由于后续请求中不会传递响应头,因此令牌只会传递给客户端一次。然后,客户端将通过在请求标头中设置令牌来使用它来发出进一步的请求。
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a header in the response
response = HttpResponseRedirect('/accounts/profile/')
response['X-Auth-Token'] = 'my_server_access_token'
# can also use the below name as 'X-' prefixed headers are deprecated
# response['Auth-Token'] = 'my_server_access_token'
return response
然后,客户端可以从标头中检索令牌并使用此令牌发出进一步的请求。在进一步的请求中,他必须在请求标头中发送访问令牌。
方法 2:将 server_access_token 设置为 cookie
另一种选择是在您的回复中设置server_access_token cookie,如提到的@Ben。
response.set_cookie() 会在响应中设置server_access_token cookie,然后客户端可以读取 cookie 并将其发送到请求标头中的进一步请求中。
class MySocialApplicationRedirectView(View):
def get(self, request, *args, **kwargs):
# Here, write your code to fetch the {application}'s access token,
# creating a new user with your server's access token, and then
# associating it with {application}'s access token
# assign the response to a variable and set the access token as a cookie in the response object
response = HttpResponseRedirect('/accounts/profile/')
response.set_cookie(key, value='my_server_access_token', ..other parameters )
return response
注意:为了安全起见,所有请求(获取和使用令牌)都必须使用 HTTPS 端点。