Note that there are some explanatory texts on larger screens.

plurals
  1. POPerforming POST on a URL string in Django
    primarykey
    data
    text
    <p>I'm using the LinkedIn Authentication at: <a href="http://developer.linkedin.com/documents/authentication" rel="nofollow">http://developer.linkedin.com/documents/authentication</a> and I need to make a post. I'm familiar with making a GET request for example as: </p> <pre><code>client = oauth.Client(consumer, access_token) resp,content = client.request("http://api.linkedin.com/v1/people/~?format=json", "GET", "") </code></pre> <p>Which used Oauth authentication. However for the Post part of the authentication, I have to POST an example string such as: </p> <pre><code>https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code &amp;code=AUTHORIZATION_CODE &amp;redirect_uri=YOUR_REDIRECT_URI &amp;client_id=YOUR_API_KEY &amp;client_secret=YOUR_SECRET_KEY </code></pre> <p>and my simple question relates to understanding what the best way to do this is - without using the Oauth (assuming that is the correct approach), since I have returned the AUTHORIZATION_CODE. Apologies if this is a simple question.</p> <p>So in other words, I have a string of the above form, now, How do I perform a post?</p> <p>I've tried using cURL. But I find the code fails at the post line: <code>c.perform()</code>. Here is the code I am using: </p> <pre><code> 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&amp;code='+accesscode+'&amp;redirect_uri='+redirect_uri+'&amp;client_id='+consumer_key+'&amp;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() </code></pre> <p>I'm getting the following error:</p> <pre><code>error at /loginsuccess/ (22, 'The requested URL returned error: 400 Bad Request') </code></pre> <p>Here is the code for how I am obtaining the first stage of the Authentication (as is suggested by the LI documentation):</p> <pre><code>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&amp;client_id=c3skrqz5wqmm&amp;scope=r_fullprofile&amp;state=DCEEFWF45453sdffef425&amp;redirect_uri=" + redirect_uri return HttpResponseRedirect(codeURL) </code></pre> <p>Here is the latest traceback that I am getting after using the code suggested below to post the data. I have a number of questions that I will outline after the trace.</p> <p>Error:</p> <p>HTTPConnectionPool(host='127.0.0.1', port=8888): Max retries exceeded with url: <a href="https://api.linkedin.com/uas/oauth/accessToken" rel="nofollow">https://api.linkedin.com/uas/oauth/accessToken</a> (Caused by : [Errno 111] Connection refused)</p> <p>Environment:</p> <pre><code>Request Method: GET Request URL: http://127.0.0.1:9000/loginsuccess/?code=AQQKMnhjMNKFEu08DkftejkNLQgSz-mIsMZtE36rGxrLfycx331iLXeC3LdAq63OTQeATAEXrA7FxgHSJWVHyh_iWyIEzVR6jp4zJsz41tpRipS14RE&amp;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 &lt;class 'socket.error'&gt;: [Errno 111] Connection refused) </code></pre> <p>Plus with the request information there is still no POST data it would seem:</p> <p>Request information</p> <p>GET Variable Value state<br> u'XX' code<br> u'XX' POST No POST data FILES No FILES data</p> <p>Here is more details.</p> <pre><code>Request Method: GET Request URL: http://127.0.0.1:9000/loginsuccess/?code=XXX&amp;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 &lt;class 'socket.error'&gt;: [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 </code></pre> <hr> <pre><code>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&amp;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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload