|
| 1 | +import os |
| 2 | +import pickle |
| 3 | +import requests |
| 4 | +from requests_oauthlib import OAuth1Session |
| 5 | +from google.auth.transport.requests import Request |
| 6 | +from google_auth_oauthlib.flow import InstalledAppFlow |
| 7 | +from googleapiclient.discovery import build |
| 8 | +from urllib.parse import urlparse, parse_qs, urlencode |
| 9 | +from announce import const |
| 10 | + |
| 11 | + |
| 12 | +def refresh_linkedin(sessions, path): |
| 13 | + API_KEY = os.environ.get("LINKEDIN_CLIENT_ID") |
| 14 | + API_SECRET = os.environ.get("LINKEDIN_CLIENT_SECRET") |
| 15 | + RETURN_URL = "http://localhost:9126/code" |
| 16 | + session = sessions.get("linkedin") |
| 17 | + headers = {"Authorization": f"Bearer {session.get('access_token')}"} |
| 18 | + r = requests.get("https://api.linkedin.com/v2/me", headers=headers) |
| 19 | + if r.json().get("id"): |
| 20 | + return |
| 21 | + |
| 22 | + url = "https://www.linkedin.com/oauth/v2/authorization/?" + urlencode( |
| 23 | + { |
| 24 | + "response_type": "code", |
| 25 | + "client_id": API_KEY, |
| 26 | + "redirect_uri": RETURN_URL, |
| 27 | + "scope": "w_member_social r_liteprofile", |
| 28 | + } |
| 29 | + ) |
| 30 | + print("Visit this url:", url) |
| 31 | + q = input("Enter redirected url: ") |
| 32 | + code = parse_qs(urlparse(q).query).get("code")[0] |
| 33 | + r = requests.get( |
| 34 | + "https://www.linkedin.com/oauth/v2/accessToken", |
| 35 | + params={ |
| 36 | + "grant_type": "authorization_code", |
| 37 | + "code": code, |
| 38 | + "redirect_uri": RETURN_URL, |
| 39 | + "client_id": API_KEY, |
| 40 | + "client_secret": API_SECRET, |
| 41 | + }, |
| 42 | + ) |
| 43 | + access_token = r.json().get("access_token") |
| 44 | + sessions["linkedin"] = {"access_token": access_token, "code": code} |
| 45 | + |
| 46 | + |
| 47 | +def refresh_google(sessions, path): |
| 48 | + SCOPES = ["https://www.googleapis.com/auth/calendar"] |
| 49 | + creds = None |
| 50 | + if os.path.exists(path / "googletoken.pickle"): |
| 51 | + with open(path / "googletoken.pickle", "rb") as token: |
| 52 | + creds = pickle.load(token) |
| 53 | + # If there are no (valid) credentials available, let the user log in. |
| 54 | + if not creds or not creds.valid: |
| 55 | + if creds and creds.expired and creds.refresh_token: |
| 56 | + creds.refresh(Request()) |
| 57 | + else: |
| 58 | + flow = InstalledAppFlow.from_client_secrets_file( |
| 59 | + path / "credentials.json", SCOPES |
| 60 | + ) |
| 61 | + creds = flow.run_local_server(port=0) |
| 62 | + # Save the credentials for the next run |
| 63 | + with open(path / "googletoken.pickle", "wb") as token: |
| 64 | + pickle.dump(creds, token) |
| 65 | + service = build("calendar", "v3", credentials=creds) |
| 66 | + sessions["google"] = service |
| 67 | + |
| 68 | + |
| 69 | +def refresh_twitter(sessions, path): |
| 70 | + request_token_url = "https://api.twitter.com/oauth/request_token" |
| 71 | + access_token_url = "https://api.twitter.com/oauth/access_token" |
| 72 | + authorize_url = "https://api.twitter.com/oauth/authorize" |
| 73 | + base_url = const.tw |
| 74 | + session = sessions.get("twitter") |
| 75 | + try: |
| 76 | + params = {"include_rts": 1, "count": 10} |
| 77 | + r = session.get(f"{base_url}/statuses/home_timeline.json", params=params) |
| 78 | + print("Found cached twitter credentials") |
| 79 | + except Exception: |
| 80 | + print("Refreshing twitter auth") |
| 81 | + client_key = os.environ.get("TWITTER_CONSUMER_KEY") |
| 82 | + client_secret = os.environ.get("TWITTER_CONSUMER_SECRET") |
| 83 | + oauth = OAuth1Session(client_key=client_key, client_secret=client_secret) |
| 84 | + fetch_response = oauth.fetch_request_token(request_token_url) |
| 85 | + resource_owner_key = fetch_response.get("oauth_token") |
| 86 | + resource_owner_secret = fetch_response.get("oauth_token_secret") |
| 87 | + authorization_url = oauth.authorization_url(authorize_url) |
| 88 | + print("Please visit this url: %s", authorization_url) |
| 89 | + verifier = input("Enter pin: ") |
| 90 | + oauth = OAuth1Session( |
| 91 | + client_key, |
| 92 | + client_secret=client_secret, |
| 93 | + resource_owner_key=resource_owner_key, |
| 94 | + resource_owner_secret=resource_owner_secret, |
| 95 | + verifier=verifier, |
| 96 | + ) |
| 97 | + oauth_tokens = oauth.fetch_access_token(access_token_url) |
| 98 | + session = oauth |
| 99 | + params = {"include_rts": 1, "count": 10} |
| 100 | + r = session.get(f"{base_url}/statuses/home_timeline.json", params=params) |
| 101 | + |
| 102 | + sessions["twitter"] = session |
| 103 | + |
| 104 | + |
| 105 | +def get_sessions(path): |
| 106 | + sessions = {} |
| 107 | + if os.path.exists(path / "sessions.pickle"): |
| 108 | + with open(path / "sessions.pickle", "rb") as fl: |
| 109 | + sessions = pickle.load(fl) |
| 110 | + # TODO(thesage21): linkedin app permissions need approval |
| 111 | + refresh_linkedin(sessions, path) |
| 112 | + # refresh_twitter(sessions, path) |
| 113 | + # refresh_google(sessions, path) |
| 114 | + # ---------------------------- |
| 115 | + with open(path / "sessions.pickle", "wb") as fl: |
| 116 | + pickle.dump(sessions, fl) |
| 117 | + return sessions |
0 commit comments