def refresh(): ''' Exchange refresh token for a new access token Send: client_id, client_secret, redirect_uri, refresh_token, grant_type Receive: access_token, token_type, expire_in, refresh_token, xoauth_yahoo_guid Note: only the access_token will change (refresh_token does not change) ''' client = WebApplicationClient(app.config['CLIENT_ID']) req = client.prepare_refresh_token_request( yahoo_oauth2.request_token_url, refresh_token=current_user.user_refresh_token, client_id=app.config['CLIENT_ID'], client_secret=app.config['CLIENT_SECRET'], redirect_uri=yahoo_oauth2.redirect_url) token_url, headers, body = req resp = requests.post(token_url, headers=headers, data=body) if resp.status_code == 400: abort(400, resp.json()['error']) #update the user object with the (response) token data current_user.set_oauth_tokens(resp.json()) #permanently store user's oauth credentials db.session.add(current_user) db.session.commit() return redirect(url_for('teams'))
def _get_access_token(self, client_id, client_secret, refresh_token): oauth = WebApplicationClient(client_id, refresh_token=refresh_token) url, headers, body = oauth.prepare_refresh_token_request( f"{self.api_base}/oauth2/token", client_id=client_id, client_secret=client_secret) req = urllib.request.Request(url, body.encode(), headers=headers) with urllib.request.urlopen(req) as res: oauth.parse_request_body_response(res.read()) return oauth.access_token