简体   繁体   English

Python使用三个参数请求库HTTPBasicAuth

[英]Python requests library HTTPBasicAuth with three parameters

I'm trying to use Instapaper's simple developer api to add a url to my bookmarks using python and the requests library. 我正在尝试使用Instapaper的简单开发人员api使用python和请求库为我的书签添加URL。 To authenticate the username and password all works well. 要验证用户名和密码,一切正常。

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不能采取第三个参数,有没有人知道这样做的方法?

HTTPBasicAuth() only ever takes username and password arguments. HTTPBasicAuth() 永远只需要用户名和密码参数。 There is no 3rd argument, full-stop. 没有第三个参数,全站。

HTTP Basic Authentication adds an extra header to the request; HTTP基本身份验证为请求添加了额外的标头; this information is kept separate from the GET or POST parameters. 此信息与GET或POST参数分开。 When you use this form of authentication, you do not need to pass any username and password parameters to the API methods either. 使用这种形式的身份验证时,您也不需要将任何usernamepassword参数传递给API方法。

To add a bookmark, pass in the url parameter 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. request.get requests.get()requests.post()都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM