我是其余API的新手,我正在尝试建立OAuth握手,并且在请求请求令牌方面需要帮助。我在Python中使用requests_oauthlib模块。这是示例代码,它正在返回Response [400]。
consumer_key,consumer_secret和request_url均已正确加载。我让我的代码可以使用其他Auth模块工作。有人可以解释什么是HTTP标头,以及如何在GET请求中使用它们吗?
from requests_oauthlib import OAuth1
from variables import *
oauth = OAuth1(consumer_key, client_secret = consumer_secret)
request_token = requests.get(request_url, auth=oauth, params={'oauth_callback':'oob', 'format':'json'})
print request_token
答案 0 :(得分:0)
请求:您的计算机通常在端口443
或80
上向另一台计算机发送http消息
响应:服务器侦听任何连接请求,并在了解消息后做出响应。
例如telnet stackoverflow.com 80
,您可以输入
GET /questions/52350391/can-someone-explain-get-requests-specifically-the-http-header HTTP/2
Host: stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*
然后按两次Enter键以结束请求标头,此时服务器将做出响应:
➜ mysite telnet stackoverflow.com 80
Trying 151.101.1.69...
Connected to stackoverflow.com.
Escape character is '^]'.
GET /questions/52350391/can-someone-explain-get-requests-specifically-the-http- header HTTP/2
Host: stackoverflow.com
User-Agent: curl/7.54.0
Accept: */*
HTTP/1.1 301 Moved Permanently
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-
X-Request-Guid: xxx
Content-Security-Policy: upgrade-insecure-requests
Accept-Ranges: bytes
Age: 0
Content-Length: 217
Accept-Ranges: bytes
Date: Sun, 16 Sep 2018 03:29:16 GMT
Via: 1.1 varnish
Age: 0
Connection: close
X-Served-By: cache-ord1744-ORD
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1537068557.736123,VS0,VE25
Vary: Fastly-SSL
X-DNS-Prefetch-Control: off
Set-Cookie: prov=xxx; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-">here</a>.</h2>
</body></html>
Connection closed by foreign host.
然后telnet会话打印出服务器的响应并关闭连接。响应将包括几部分,响应头和响应主体。
您的示例可能类似于:
GET /some/oauth/api?oauth_callback=oob&format=json
Host: someplace.com
Authorization: Bearer asdfasdfasdfasdf
HTTP/1.1 200 OK
Content-Type: application/json
Cache-Control: no-store
Pragma: no-cache
{
"access_token":"sdfasasdfasdf",
"token_type":"bearer",
"expires_in":3600,
"refresh_token":"asdfasdfasdfasdf",
"scope":"create"
}
还要签出:
curl -Lv https://stackoverflow.com/questions/52350391/can-someone-explain-get-requests-specifically-the-http-header | head -n 100
相关: