gpt4 book ai didi

python - 在 Django 中对 URL 字符串执行 POST

转载 作者:太空狗 更新时间:2023-10-29 22:08:05 24 4
gpt4 key购买 nike

我在以下位置使用 LinkedIn 身份验证:http://developer.linkedin.com/documents/authentication我需要发帖。我熟悉发出 GET 请求,例如:

client = oauth.Client(consumer, access_token)
resp,content = client.request("http://api.linkedin.com/v1/people/~?format=json", "GET", "")

其中使用了 Oauth 身份验证。但是对于身份验证的 Post 部分,我必须 POST 一个示例字符串,例如:

https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
&code=AUTHORIZATION_CODE
&redirect_uri=YOUR_REDIRECT_URI
&client_id=YOUR_API_KEY
&client_secret=YOUR_SECRET_KEY

我的简单问题涉及了解执行此操作的最佳方法是什么 - 不使用 Oauth(假设这是正确的方法),因为我已经返回了 AUTHORIZATION_CODE。如果这是一个简单的问题,我们深表歉意。

所以换句话说,我有一个上述形式的字符串,现在,我如何执行一个帖子?

我试过使用 cURL。但我发现代码在 post 行失败:c.perform()。这是我使用的代码:

   accesscode = request.GET.get('code')
redirect_uri = 'http://www.example.com'
url_post = 'https://www.linkedin.com/uas/oauth2/accessToken'
postdata = urllib2.quote('grant_type=authorization_code&code='+accesscode+'&redirect_uri='+redirect_uri+'&client_id='+consumer_key+'&client_secret='+consumer_secret)
#return HttpResponse(postdata)

c = pycurl.Curl()
c.setopt(c.URL, url_post)
c.setopt(c.POSTFIELDS, postdata)
c.setopt(c.VERBOSE, True)
c.setopt(c.FAILONERROR, True)
c.perform()

我收到以下错误:

error at /loginsuccess/
(22, 'The requested URL returned error: 400 Bad Request')

这是我如何获得身份验证第一阶段的代码(正如 LI 文档所建议的那样):

def login(request):
redirect_uri = urllib2.quote('http://127.0.0.1:8080/loginsuccess')
codeURL = "https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=c3skrqz5wqmm&scope=r_fullprofile&state=DCEEFWF45453sdffef425&redirect_uri=" + redirect_uri
return HttpResponseRedirect(codeURL)

这是我在使用下面建议的代码发布数据后得到的最新回溯。我有一些问题,我将在追踪之后列出这些问题。

错误:

HTTPConnectionPool(host='127.0.0.1', port=8888): 最大重试次数超过 url: https://api.linkedin.com/uas/oauth/accessToken (原因:[Errno 111] 连接被拒绝)

环境:

Request Method: GET
Request URL: http://127.0.0.1:9000/loginsuccess/?code=AQQKMnhjMNKFEu08DkftejkNLQgSz-mIsMZtE36rGxrLfycx331iLXeC3LdAq63OTQeATAEXrA7FxgHSJWVHyh_iWyIEzVR6jp4zJsz41tpRipS14RE&state=DCEEFWF45453sdffef425

Django Version: 1.4.5
Python Version: 2.7.4
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/home/brett/LinkedIn/brett/brett/views.py" in loginsuccess
68. r = requests.post(access_token_url, data=postdata)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py" in post
88. return request('post', url, data=data, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/api.py" in request
44. return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py" in request
335. resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py" in send
438. r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py" in send
327. raise ConnectionError(e)

Exception Type: ConnectionError at /loginsuccess/
Exception Value: HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: https://api.linkedin.com/uas/oauth/accessToken (Caused by <class 'socket.error'>: [Errno 111] Connection refused)

加上请求信息,似乎仍然没有 POST 数据:

请求信息

得到变量值状态
你'XX'代码
你'XX'邮政无更新数据文件没有文件数据

这里有更多详细信息。

Request Method: GET
Request URL: http://127.0.0.1:9000/loginsuccess/?code=XXX&state=XXX
Django Version: 1.4.5
Exception Type: ConnectionError
Exception Value:
HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: https://api.linkedin.com/uas/oauth/accessToken (Caused by <class 'socket.error'>: [Errno 111] Connection refused)
Exception Location: /usr/local/lib/python2.7/dist-packages/requests/adapters.py in send, line 327
Python Executable: /usr/bin/python

Fiddler results:

HTTP/1.1 302 Found
Server: Apache-Coyote/1.1
P3P: CP="CAO DSP COR CUR ADMi DEVi TAIi PSAi PSDi IVAi IVDi CONi OUR DELi SAMi UNRi PUBi OTRi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT POL PRE"
X-LI-UUID: dI14TYAfd/RHJ8mVmAN8Gg==
Location: http://127.0.0.1:9000/loginsuccess?code=XX=XX
Content-Language: en-US
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Thu, 05 Sep 2013 19:26:48 GMT
X-Li-Fabric: PROD-ELA4
Set-Cookie: _lipt=deleteMe; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
Set-Cookie: leo_auth_token="XX"; Version=1; Max-Age=1799; Expires=Thu, 05-Sep-2013 19:56:47 GMT; Path=/
Set-Cookie: sl="delete me"; Version=1; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
Set-Cookie: sl="delete me"; Version=1; Domain=.www.linkedin.com; Max-Age=0; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/
Set-Cookie: lang="v=2&lang=en-us"; Version=1; Domain=linkedin.com; Path=/
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Age: 1
Transfer-Encoding: chunked
Connection: keep-alive
X-Li-Pop: PROD-IDB2

14
�����������������
0

最佳答案

看看requests (使用 pip install requests 安装),只需几行代码即可轻松发出 POST 请求:

import requests

accesscode = request.GET.get('code')
redirect_uri = 'http://www.example.com'
url = 'https://www.linkedin.com/uas/oauth2/accessToken'
# ... your code ...

postdata = {
'grant_type': 'authorization_code',
'code': accesscode,
'redirect_uri': redirect_uri,
'client_id': consumer_key,
'client_secret': consumer_secret,
}

r = requests.post(url, data=postdata)
print(r.status_code)
print(r.text)

您可以在 Python shell 中尝试此操作,因为我认为这样做很简单。

关于python - 在 Django 中对 URL 字符串执行 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com