this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add support for app-only bearer tokens

Previously, twitter-to-sqlite only supported OAuth1 authentication, and
the token must be on behalf of a user. However, Twitter also supports
application-only bearer tokens, documented here:
https://developer.twitter.com/en/docs/authentication/oauth-2-0/bearer-tokens
This PR adds support to twitter-to-sqlite for using application-only
bearer tokens. To use, the auth.json file just needs to contain a
"bearer_token" key instead of "api_key", "api_secret_key", etc.

+29 -6
+7
README.md
··· 13 13 14 14 - [How to install](#how-to-install) 15 15 - [Authentication](#authentication) 16 + * [App-only authentication](#app-only-authentication) 16 17 - [Retrieving tweets by specific accounts](#retrieving-tweets-by-specific-accounts) 17 18 - [Retrieve user profiles in bulk](#retrieve-user-profiles-in-bulk) 18 19 - [Retrieve tweets in bulk](#retrieve-tweets-in-bulk) ··· 63 64 Access token secret: xxx 64 65 65 66 This will create a file called `auth.json` in your current directory containing the required values. To save the file at a different path or filename, use the `--auth=myauth.json` option. 67 + 68 + ### App-only authentication 69 + 70 + Authentication using an [app-only Bearer Token](https://developer.twitter.com/en/docs/authentication/oauth-2-0/bearer-tokens) is also possible. If you already have a bearer token, you can create an `auth.json` file directly: 71 + 72 + {"bearer_token": "AAAA...cE3F"} 66 73 67 74 ## Retrieving tweets by specific accounts 68 75
+22 -6
twitter_to_sqlite/utils.py
··· 10 10 import zipfile 11 11 12 12 from dateutil import parser 13 + from requests import Session 14 + from requests.auth import AuthBase 13 15 from requests_oauthlib import OAuth1Session 14 16 import sqlite_utils 15 17 ··· 66 68 ) 67 69 68 70 71 + class BearerTokenAuth(AuthBase): 72 + def __init__(self, bearer_token): 73 + self.bearer_token = bearer_token 74 + 75 + def __call__(self, r): 76 + r.headers["Authorization"] = "Bearer " + self.bearer_token 77 + return r 78 + 79 + 69 80 def session_for_auth(auth): 70 - return OAuth1Session( 71 - client_key=auth["api_key"], 72 - client_secret=auth["api_secret_key"], 73 - resource_owner_key=auth["access_token"], 74 - resource_owner_secret=auth["access_token_secret"], 75 - ) 81 + if "bearer_token" in auth: 82 + session = Session() 83 + session.auth = BearerTokenAuth(auth["bearer_token"]) 84 + return session 85 + else: 86 + return OAuth1Session( 87 + client_key=auth["api_key"], 88 + client_secret=auth["api_secret_key"], 89 + resource_owner_key=auth["access_token"], 90 + resource_owner_secret=auth["access_token_secret"], 91 + ) 76 92 77 93 78 94 def fetch_user_list_chunks(