的urllib2引发403错误而卷曲同一请求工作正常 [英] Urllib2 raises 403 error while the same request in curl works fine

查看:153
本文介绍了的urllib2引发403错误而卷曲同一请求工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将我tranfoms这curl命令:

 卷曲-v -d email=onlinecrapbox@gmail.com -d密码=输入mypassword -X POST https://www.toggl.com/api/v6/sessions.json

成urlib2?

这是为什么不工作:

  URL ='https://www.toggl.com/api/v6/sessions.json
用户名='onlinecrapbox@gmail.com
密码='输入mypassword帕斯曼= urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(无,网址,用户名,密码)authhandler = urllib2.HTTPBasicAuthHandler(帕斯曼)首战= urllib2.build_opener(authhandler)urllib2.install_opener(揭幕战)
pagehandle = urllib2.urlopen(URL)

它给了我这个错误:

 回溯(最后最近一次调用):
  文件/Users/jorrit/virtualenvs/tiddle/tiddle/troggle/tests.py,第16行,在test_get_troggle_connection
    get_projects()
  文件/Users/jorrit/virtualenvs/tiddle/tiddle/troggle/views.py,第29行,在get_projects
    pagehandle = urllib2.urlopen(URL)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,线路126在的urlopen
    返回_opener.open(URL,数据,超时)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,400线,在开
    响应=甲基(REQ,响应)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,线路513,在HTTP_RESPONSE
    HTTP,请求,响应,code,味精,HDRS)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,线路438,错误
    返回self._call_chain(*参数)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,线路372,在_call_chain
    结果= FUNC(*参数)
  文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py,线路521,在http_error_default
    引发HTTPError(req.get_full_url(),code,味精,HDRS,FP)
HTTPError这样的:HTTP错误403:禁止


解决方案

与请求包,code将是:

 进口要求URL ='https://www.toggl.com/api/v6/sessions.json
有效载荷= {'邮件':'onlinecrapbox@gmail.com',
           '密码':'MYPASSWORD'}R = requests.post(URL,数据=有效载荷)

how would i tranfoms this curl command:

curl -v -d email=onlinecrapbox@gmail.com -d password=mypassword -X POST https://www.toggl.com/api/v6/sessions.json

into urlib2?

Why is this not working:

url=       'https://www.toggl.com/api/v6/sessions.json'
username = 'onlinecrapbox@gmail.com'
password = 'mypassword'

passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)

authhandler = urllib2.HTTPBasicAuthHandler(passman)

opener = urllib2.build_opener(authhandler)

urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(url)

it gives me this error:

Traceback (most recent call last):
  File "/Users/jorrit/virtualenvs/tiddle/tiddle/troggle/tests.py", line 16, in test_get_troggle_connection
    get_projects()
  File "/Users/jorrit/virtualenvs/tiddle/tiddle/troggle/views.py", line 29, in get_projects
    pagehandle = urllib2.urlopen(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden

解决方案

with requests package, the code would be:

import requests

url = 'https://www.toggl.com/api/v6/sessions.json'
payload = {'email': 'onlinecrapbox@gmail.com',
           'password': 'mypassword'}

r = requests.post(url, data=payload)

这篇关于的urllib2引发403错误而卷曲同一请求工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆