使用Python中的服务帐户创建DfpClient
我使用GoogleDFPAPI收集我们网站上点击广告的一些统计数据。使用Python中的服务帐户创建DfpClient,python,yaml,jwt,google-dfp,service-accounts,Python,Yaml,Jwt,Google Dfp,Service Accounts,我使用GoogleDFPAPI收集我们网站上点击广告的一些统计数据。 代码是用Python编写的。目前,我正在尝试升级代码以使用oAuth 2。 由于代码每天自动运行,没有任何用户参与,因此我创建了一个 “我的谷歌项目”下的服务帐户,并将该帐户添加到双击中 我们公司的出版商网络。基于web上的示例代码,我写了以下内容: import httplib2 from oauth2client.client import SignedJwtAssertionCredentials from apic
代码是用Python编写的。目前,我正在尝试升级代码以使用oAuth 2。
由于代码每天自动运行,没有任何用户参与,因此我创建了一个
“我的谷歌项目”下的服务帐户,并将该帐户添加到双击中
我们公司的出版商网络。基于web上的示例代码,我写了以下内容:
import httplib2
from oauth2client.client import SignedJwtAssertionCredentials
from apiclient.discovery import build
from googleads.dfp import DfpClient
GOOGLE_DFP_SCOPE="https://www.googleapis.com/auth/dfp"
API_VERSION="v201411"
KEY_FILE="*******.p12"
ACCT_EMAIL="************************@developer.gserviceaccount.com"
NETWORK_CODE="**********"
with open(KEY_FILE) as config_file:
my_private_key = config_file.read()
credentials = SignedJwtAssertionCredentials(service_account_name=ACCT_EMAIL, private_key=my_private_key,scope=GOOGLE_DFP_SCOPE)
http = httplib2.Http()
http_auth = credentials.authorize(http)
dfp_client = build(serviceName='dfp',version=API_VERSION,http=http_auth)
此代码似乎不正确,因为尚未传递网络代码
代码中的任何地方。此外,它会失败,并显示以下消息:
apiclient.errors.UnknownApiNameOrVersion:名称:dfp版本:v201411。
此外,下面一行:
dfp_client = DfpClient.LoadFromStorage()
不适用于我的案例,因为,这是基于googleads.yaml的
仅针对具有客户端密码(而非P12私钥)的web应用程序帐户进行格式化
有什么建议吗?谢谢。Apiclient.discovery使用默认值检查服务。 但我并没有找到为出版商提供双击服务的服务 我使用此代码将API与Oauth2一起使用。使用
导入json
导入请求
进口烧瓶
从谷歌进口dfp
从googleads导入oauth2
app=烧瓶。烧瓶(\uuuuu名称\uuuuuuu)
客户端ID=“”
CLIENT_SECRET=''#从实际应用程序中的文件或环境变量读取
范围=https://www.googleapis.com/auth/dfp'
重定向_URI=''
应用程序名称='DFP API服务'
网络_代码=“”
@应用程序路径(“/”)
def index():
如果“凭据”不在flask.session中:
返回flask.redirect(flask.url_用于('oauth2callback'))
credentials=json.loads(flask.session['credentials'])
如果凭证['expires_in']您是正确的。创建dfp客户机时,必须传递网络代码。并且版本不是必需的。尝试使用以下代码在python中创建客户机
import os
from googleads import oauth2
from googleads import dfp
def get_dfp_client():
application_name = "Your application name" # from google developer console. eg: Web Client
network_code = ********
private_key_password = 'notasecret'
key_file = os.path.join('path/to/p12file')
service_account_email = '****@***.iam.gserviceaccount.com'
# create oath2 client(google login)
oauth2_client = oauth2.GoogleServiceAccountClient(
oauth2.GetAPIScope('dfp'), service_account_email, key_file)
dfp_client = dfp.DfpClient(oauth2_client, application_name, network_code)
return dfp_client
client = get_dfp_client()
如果您需要更多澄清,请发表评论
更新
googleads将模块dfp重命名为ad_经理,docs–Gocht
哪个googleads版本有dfp?为将来的引用导入dfp时出错:Googleas将重命名的模块dfp
导入到ad\U管理器
,文档
import os
from googleads import oauth2
from googleads import dfp
def get_dfp_client():
application_name = "Your application name" # from google developer console. eg: Web Client
network_code = ********
private_key_password = 'notasecret'
key_file = os.path.join('path/to/p12file')
service_account_email = '****@***.iam.gserviceaccount.com'
# create oath2 client(google login)
oauth2_client = oauth2.GoogleServiceAccountClient(
oauth2.GetAPIScope('dfp'), service_account_email, key_file)
dfp_client = dfp.DfpClient(oauth2_client, application_name, network_code)
return dfp_client
client = get_dfp_client()