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

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>
主站蜘蛛池模板: 阿坝| 沂源县| 理塘县| 西青区| 尼勒克县| 台中县| 牙克石市| 黄大仙区| 徐州市| 海安县| 嘉祥县| 博罗县| 黑水县| 北海市| 汉阴县| 潮安县| 铜川市| 商水县| 乐陵市| 吉林省| 南靖县| 井陉县| 伊川县| 辉南县| 京山县| 昭平县| 皮山县| 池州市| 大悟县| 龙南县| 厦门市| 富川| 交城县| 临泽县| 肥乡县| 南皮县| 杨浦区| 南康市| 左云县| 轮台县| 新平|