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

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>
主站蜘蛛池模板: 辰溪县| 松阳县| 深水埗区| 酉阳| 山阴县| 清丰县| 乐陵市| 海晏县| 璧山县| 台南市| 灵台县| 濮阳市| 浪卡子县| 延长县| 阳朔县| 宜都市| 株洲县| 曲麻莱县| 怀集县| 横山县| 亚东县| 杨浦区| 南通市| 监利县| 兰考县| 四会市| 会东县| 海晏县| 理塘县| 嵩明县| 潼关县| 新绛县| 勃利县| 阿图什市| 潜山县| 射洪县| 永胜县| 洛宁县| 普陀区| 盘山县| 伊通|