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

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>
主站蜘蛛池模板: 延吉市| 固安县| 婺源县| 宁都县| 车险| 峨山| 大安市| 锡林郭勒盟| 德保县| 承德市| 镇沅| 布拖县| 精河县| 新龙县| 封丘县| 宁德市| 永和县| 永和县| 安塞县| 丰都县| 山东省| 望奎县| 页游| 宣城市| 永兴县| 调兵山市| 亚东县| 孟津县| 辽中县| 平利县| 杭锦后旗| 贺州市| 平泉县| 钟祥市| 米脂县| 巴彦县| 南溪县| 辽宁省| 商丘市| 富锦市| 新乡市|