您不应该在查询字符串中传递访问令牌,如/?token=my_token
。它不是一个安全的方式,绝对不推荐。
您可以使用其他一些方法是:
方法1:在响应头
您可以设置访问令牌的响应报头,并使用HTTPS协议发送设置server_access_token
。
令牌将被发送一次并被客户端使用。由于响应标头未在后续请求中传递,令牌只会传递给客户端一次。然后,客户端将通过在请求标头中设置令牌来使用它来发出进一步的请求。
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
饼干作为@Ben提到你的回应。
response.set_cookie()
将在响应中设置server_access_token
cookie,然后客户端可以读取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端点。
什么是客户打算与服务器的访问令牌呢? –
我将使用服务器的访问令牌请求数据,如果数据位于{application}上,我的服务器将使用{application}的访问令牌请求数据并返回给客户端。 –