- Python Programming Blueprints
- Daniel Furtado Marcus Pennington
- 387字
- 2021-06-24 18:53:52
Creating the Flask application
Perfect, now let's add another file called twitter_auth.py in the twittervotes directory. We are going to create three functions in it but, first, let's add some imports:
from urllib.parse import parse_qsl
import yaml
from flask import Flask
from flask import render_template
from flask import request
import oauth2 as oauth
from core import read_config
from core.models import RequestToken
First, we import the parser_qls from the urllib.parse module to parse the returned query string, and the yaml module so we can read and write YAML configuration files. Then we import everything we need to build our Flask application. The last third-party module that we are going to import here is the oauth2 module, which will help us to perform the OAuth authentication.
Lastly, we import our function read_config and the RequestToken namedtuple that we just created.
Here, we create our Flask app and a few global variables that will hold values for the client, consumer, and the RequestToken instance:
app = Flask(__name__)
client = None
consumer = None
req_token = None
The first function that we are going to create is a function called get_req_token with the following content:
def get_oauth_token(config):
global consumer
global client
global req_token
consumer = oauth.Consumer(config.consumer_key,
config.consumer_secret)
client = oauth.Client(consumer)
resp, content = client.request(config.request_token_url, 'GET')
if resp['status'] != '200':
raise Exception("Invalid response
{}".format(resp['status']))
request_token = dict(parse_qsl(content.decode('utf-8')))
req_token = RequestToken(**request_token)
This function gets as argument an instance to the configuration and the global statements say to the interpreter that the consumer, client, and req_token used in the function will be referencing the global variables.
We create a consumer object using the consumer key and the consumer secret that we obtained when the Twitter app was created. When the consumer is created, we can pass it to the client function to create the client, then we call the function request, which, as the name suggests, will perform the request to Twitter, passing the request token URL.
When the request is complete, the response and the content will be stored in the variables resp and content. Right after that, we test whether the response status is not 200 or HTTP.OK; in that case, we raise an exception, otherwise we parse the query string to get the values that have been sent back to us and create a RequestToken instance.
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- 黑客攻防實(shí)戰(zhàn)技術(shù)完全手冊(cè):掃描、嗅探、入侵與防御
- 數(shù)據(jù)通信網(wǎng)絡(luò)實(shí)踐:基礎(chǔ)知識(shí)與交換機(jī)技術(shù)
- Cisco OSPF命令與配置手冊(cè)
- 物聯(lián)網(wǎng)網(wǎng)絡(luò)安全及應(yīng)用
- 計(jì)算機(jī)網(wǎng)絡(luò)安全實(shí)訓(xùn)教程(第二版)
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 局域網(wǎng)組建、管理與維護(hù)項(xiàng)目教程(Windows Server 2003)
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- Wireshark網(wǎng)絡(luò)分析就這么簡(jiǎn)單
- Yii Application Development Cookbook(Second Edition)
- OMNeT++與網(wǎng)絡(luò)仿真
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- 面向5G-Advanced的關(guān)鍵技術(shù)