官术网_书友最值得收藏!

Performing authentication

In this section, we are going to create the program that will perform authentication for us so we can use the Twitter API. We are going to do that using a simple Flask application that will expose two routes. The first is the root /, which will just load and render a simple HTML template with a button that will redirect us to the Twitter authentication dialog.

The second route that we are going to create is /callback. Remember when we specified the callback URL in the Twitter app configuration? This is the route that will be called after we authorize the app. It will return an authorization token that will be used to perform requests to the Twitter API. So let's get right into it!

Before we start implementing the Flask app, we need to add another model to our model's module. This model will represent the request authorization data. Open the models.py file in twittervotes/core/models and add the following code:

RequestToken = namedtuple('RequestToken', ['oauth_token',
'oauth_token_secret',
'oauth_callback_confirmed'])

This will create a namedtuple called RequestToken with the fields oauth_token, oauth_token_secret, and outh_callback_confirmed; this data will be necessary for us to perform the second step of the authentication.

Lastly, open the __init__.py file in the twittervotes/core/models directory and let's import the RequestToken namedtuple that we just created, as follows:

from .models import RequestToken

Now that we have the model in place, let's start creating the Flask application. Let's add a very simple template to show a button that will start the authentication process.

Create a new directory in the twittervotes directory called templates and create a file called index.html  with the following content:

<html>
<head>
</head>
<body>
<a href="{{link}}"> Click here to authorize </a>
</body>
</html>
主站蜘蛛池模板: 赤壁市| 墨脱县| 库尔勒市| 武隆县| 遵化市| 宜春市| 噶尔县| 昌黎县| 沁水县| 沧州市| 抚顺市| 兴文县| 镇巴县| 剑河县| 正宁县| 抚松县| 丽水市| 诸城市| 东山县| 确山县| 滦南县| 什邡市| 册亨县| 华安县| 富川| 蓝田县| 天水市| 精河县| 古蔺县| 寿阳县| 乐业县| 营山县| 龙江县| 禄丰县| 濮阳市| 广西| 宜宾市| 瑞安市| 安顺市| 苍山县| 泾源县|