Python 使用三个参数请求库 HTTPBasicAuth

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14797809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:28:08  来源:igfitidea点击:

Python requests library HTTPBasicAuth with three parameters

pythonhttp

提问by Wilberto

I'm trying to use Instapaper's simple developer apito add a url to my bookmarks using python and the requests library. To authenticate the username and password all works well.

我正在尝试使用Instapaper 的简单开发人员 api使用 python 和请求库将 url 添加到我的书签中。验证用户名和密码一切正常。

import requests
from requests.auth import HTTPBasicAuth
requests.get('https://www.instapaper.com/api/authenticate', auth=HTTPBasicAuth('username', 'password'))

But when trying to use the api to add a bookmark:

但是当尝试使用 api 添加书签时:

 requests.get('https://www.instapaper.com/api/add', auth=HTTPBasicAuth('username', 'password','websiteUrl')) 

I get error:

我得到错误:

File "instantbookmark.py", line 3, in <module>
   getA = requests.get('https://www.instapaper.com/api/add',     auth=HTTPBasicAuth('username', 'password','websiteUrl'))
TypeError: __init__() takes exactly 3 arguments (4 given)

I think this is because HTTPBasicAuth can't take a third argument, does anyone know a way to do this?

我认为这是因为 HTTPBasicAuth 不能接​​受第三个参数,有人知道这样做的方法吗?

采纳答案by Martijn Pieters

HTTPBasicAuth()only evertakes username and password arguments. There is no 3rd argument, full-stop.

HTTPBasicAuth()只有永远需要用户名和密码参数。没有第三个论点,句号。

HTTP Basic Authentication adds an extra header to the request; this information is kept separate from the GET or POST parameters. When you use this form of authentication, you do not need to pass any usernameand passwordparameters to the API methods either.

HTTP 基本身份验证为请求添加了一个额外的标头;此信息与 GET 或 POST 参数分开保存。当您使用此形式的认证,你不需要通过任何usernamepassword参数的API方法要么。

To add a bookmark, pass in the urlparameter as a POST or GET data parameter:

要添加书签,请将url参数作为 POST 或 GET 数据参数传入:

 requests.post('https://www.instapaper.com/api/add', data={'url': 'websiteUrl'}, auth=HTTPBasicAuth('username', 'password'))

Either requests.get()or requests.post()will do.

无论是requests.get()requests.post()会做。