Python requests_oauth2 使用教程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16521486/
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 22:54:23  来源:igfitidea点击:

Tutorial for using requests_oauth2

pythonrequestoauth-2.0pocket

提问by Cutú Chique?o

I am trying to use the requests-oauth library for Python to make a request to Pocket. Unfortunately the description to use this library is not very comprehensive and I am also new to use oauth2. Generally I understand the process behind it but still can not convert this to get the request to work.

我正在尝试使用 Python 的 requests-oauth 库向 Pocket 发出请求。不幸的是,使用这个库的描述不是很全面,我也是使用 oauth2 的新手。通常我理解它背后的过程,但仍然无法转换它以使请求工作。

What I am looking for is abetter documentation or some comprehensive examples to use requests-oauth2. Does anyone know.

我正在寻找的是更好的文档或一些使用 requests-oauth2 的综合示例。有人知道吗。

Especially I am not understanding what redirect_url to use when the request is made only by a script on my local machine but not a web application.

特别是当请求仅由本地计算机上的脚本而不是 Web 应用程序发出时,我不明白要使用什么 redirect_url。

采纳答案by ThePhysicist

The best source of information that I could find is the documentation (and of course the source code) of the requests-oauth2 project on Github:

我能找到的最好的信息来源是 Github 上 requests-oauth2 项目的文档(当然还有源代码):

The document also contains an "Interesting readings" section with some links to additional tutorials. For a general overview of the OAuth2 process flow, have a look at this tutorial:

该文档还包含一个“有趣的阅读”部分,其中包含一些指向其他教程的链接。有关 OAuth2 流程的一般概述,请查看本教程:

Of course there is also the "old-fashioned" python-oauth2 library, which is quite well-documented:

当然还有“老式”python-oauth2 库,它有很好的文档记录:

The documentation of the module contains an example of three-legged authentication for the Twitter API.

该模块的文档包含 Twitter API 的三足身份验证示例。

Concerning the redirect_urlfield: In general, you can use any URL you want here (even local addresses such as http://localhost/my/endpoint), the OAuth server will simply issue a HTTP 303redirect request to the client after authenticating him, which is then processed on the client-side. However, some API services (such as Twitter) will require you to specify the redict_url beforehand and will refuse some URLs (e.g. IP-based ones).

关于redirect_url字段:一般情况下,您可以在此处使用任何您想要的URL(甚至是本地地址,例如http://localhost/my/endpoint),OAuth 服务器会在对他进行身份验证后简单地向客户端发出HTTP 303重定向请求,然后在客户端进行处理——边。但是,某些 API 服务(例如 Twitter)会要求您事先指定 redict_url,并且会拒绝某些 URL(例如基于 IP 的 URL)。

回答by devin_s

If you're a fan of the requests Python library, I recommend using requests-oauthlib. It has great documentationand active developers.

如果您是 requests Python 库的粉丝,我建议您使用requests-oauthlib。它有很好的文档和活跃的开发人员。

Normally you setup the redirect_urlin your application's settings on the API providers website. It looks like Pocket only supports browser based authentication, so you'll need a web browser to generate an access token that you can then embed in your script.

通常redirect_url,您在 API 提供商网站上的应用程序设置中设置 。看起来 Pocket 只支持基于浏览器的身份验证,因此您需要一个 Web 浏览器来生成一个访问令牌,然后您可以将其嵌入到您的脚本中。