· 7 years ago · Feb 22, 2018, 06:16 AM
1# -*- coding: utf-8 -*-
2import scrapy
3from scrapy import Request
4import oauth2 as oauth
5
6
7CONSUMER_KEY = "xxxx"
8CONSUMER_SECRET = "xxxx"
9ACCESS_KEY = "xxxx"
10ACCESS_SECRET = "xxxx"
11
12
13class TwitterSpider(scrapy.Spider):
14 name = 'twitter'
15 timeline_endpoint = "https://api.twitter.com/1.1/statuses/user_timeline.json"
16
17 def create_authorizatioin_header(self, endpoint):
18 consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
19 token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
20 params = {
21 'oauth_consumer_key': CONSUMER_KEY,
22 'oauth_nonce':oauth.generate_nonce(),
23 'oauth_timestamp':str(int(time.time())),
24 'oauth_token': ACCESS_KEY,
25 'oauth_version':"1.0",
26 }
27
28 req = oauth.Request(method="POST", url=endpoint, parameters=params)
29 req.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
30 signature = req['oauth_signature'].decode("utf-8")
31 print(signature)
32 header = 'OAuth oauth_consumer_key="'+ CONSUMER_KEY +'",oauth_nonce="'+ params['oauth_nonce']+'",oauth_signature="'+ signature +'",oauth_signature_method="HMAC-SHA1",oauth_timestamp="'+ params['oauth_timestamp'] +'",oauth_token="'+ ACCESS_KEY +'",oauth_version="1.0"'
33 print(header)
34 return header
35
36 def start_requests(self):
37 yield Request(self.timeline_endpoint, method="POST", headers={
38 'Content-Type': 'application/x-www-form-urlencoded',
39 'Authorization': self.create_authorizatioin_header(self.timeline_endpoint)
40 })
41
42 def parse(self, response):
43 print(response.text)