Colab对Google AdWords进行身份验证

问题描述 投票:0回答:1

我想通过Colab笔记本(colab.research.google.com)针对Google AdWords API进行身份验证。

[目前,Google的AdWords库有一个专用的oauth2客户端googleads import oauth2http://googleads.github.io/googleads-python-lib/googleads.oauth2.GoogleOAuth2Client-class.html),该客户端需要ID和凭据,但我不想将其存储在Colab笔记本中。

Colab具有自己的Google资源google.colab.auth import authenticate_userhttps://colab.research.google.com/notebooks/io.ipynb)身份验证。要进行身份验证,我只需使用authenticate_user()并按照该流程登录即可,而无需在笔记本中存储凭据。

是否可以对Google Ads库使用Colab身份验证?

python google-colaboratory google-adwords google-oauth2
1个回答
0
投票

首先,通过pip安装googleads

!pip install googleads

其次,运行此python代码段。 “魔术”很简单。 authenticate_user()将“应用程序默认凭据(ADC)”写入ads.json,其参数可以从AdWords库中传递给OAuth2客户端。

from google.colab.auth import authenticate_user
from google.colab import drive
from google.colab import files
# Authentication for the AdWrods Stuff
from googleads import oauth2
from googleads import adwords
# required for reading the auth_info JSON
import json
# generates the adc.json
authenticate_user()
# this is the temp file where the "Application Default Credentials (ADC)" are stored.
filename = "adc.json"
# now we open the file pass the relevant information to the oauth2 client from google AdWords
with open(filename, 'r') as f:
  auth_info = json.load(f)
  client_customer_id=".-.- Client ID goes here goes here -.-."
  developer_token=".-.- Developer Token goes here -.-."
  oauth2_client = oauth2.GoogleRefreshTokenClient(
    auth_info["client_id"],
    auth_info["client_secret"],
    auth_info["refresh_token"],
  )
  adwords_client = adwords.AdWordsClient(
    developer_token,
    oauth2_client,
    client_customer_id,
  )
# Initialize appropriate service.
traffic_estimator_service = adwords_client.GetService('TrafficEstimatorService', version='v201809')
© www.soinside.com 2019 - 2024. All rights reserved.