Я использую приведенный ниже код для аутентификации на платформе Display&Video 360
. Я могу получить токен доступа, но не могу получить refre sh токен. Я использовал один и тот же код для генерации токена Access & refre sh для платформы Google Analytics
. Это сработало, но я не знаю, почему оно не работает с D&V 360
. Я должен изменить область действия и другие параметры также
Код:
from requests.adapters import HTTPAdapter
from requests_oauthlib import OAuth2Session
from urllib3.util.retry import Retry
import os
from yaml import load, dump, YAMLError
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
# OAuth endpoints given in the Google API documentation
authorization_base_url = "https://accounts.google.com/o/oauth2/v2/auth"
token_uri = "https://www.googleapis.com/oauth2/v4/token"
'''
Defines a very simple class to handle google api authorization flow
for the requests library. Includes saving the token and automatic
token refresh.
giles 2018
'''
class Authorize:
def __init__(self):
self.scope = 'https://www.googleapis.com/auth/doubleclickbidmanager'
self.token_file = 'xxx\\New Text .txt'
self.session = None
self.token = None
self.client_id = 'xxxxxxx.apps.googleusercontent.com'
self.client_secret = 'xxxxxx'
self.redirect_uri = 'https://www.example.com/callback'
self.token_uri = token_uri
self.extra = {
'client_id': self.client_id,
'client_secret': self.client_secret}
def load_token(self):
try:
with open(self.token_file, 'r') as stream:
token = load(stream, Loader=Loader)
except (YAMLError, IOError):
return None
return token
def save_token(self, token):
with open(self.token_file, 'w') as stream:
dump(token, stream, Dumper=Dumper)
os.chmod(self.token_file, 0o600)
def authorize(self):
token = self.load_token()
if token:
# force refresh on load token.expires_in = -30
# todo this is no longer in the token ??
# how to force update?
self.session = OAuth2Session(self.client_id, token=token,
auto_refresh_url=self.token_uri,
auto_refresh_kwargs=self.extra,
token_updater=self.save_token)
else:
print(self.scope)
self.session = OAuth2Session(self.client_id, scope=self.scope,
redirect_uri=self.redirect_uri,
auto_refresh_url=self.token_uri,
auto_refresh_kwargs=self.extra,
token_updater=self.save_token)
# Redirect user to Google for authorization
authorization_url, _ = self.session.authorization_url(
authorization_base_url,
access_type="offline",
prompt="select_account")
print('Please go here and authorize,', authorization_url)
# Get the authorization verifier code from the callback url
response_code = input('Paste the response token here:')
# Fetch the access token
self.token = self.session.fetch_token(
self.token_uri, client_secret=self.client_secret,
code=response_code)
print(self.token)
self.save_token(self.token)
# note this does retries on POST as well
# CAUTION - this might not be desirable for all applications
retries = Retry(total=5,
backoff_factor=0.1,
status_forcelist=[500, 502, 503, 504],
method_whitelist=frozenset(['GET', 'POST']),
raise_on_status=False)
self.session.mount('https://', HTTPAdapter(max_retries=retries))
object = Authorize()