Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将SignedJwtAssertionCredentials与Google Analytics JSON密钥文件一起使用?_Python_Google Analytics_Openssl_Google Analytics Api_Oauth2client - Fatal编程技术网

Python 如何将SignedJwtAssertionCredentials与Google Analytics JSON密钥文件一起使用?

Python 如何将SignedJwtAssertionCredentials与Google Analytics JSON密钥文件一起使用?,python,google-analytics,openssl,google-analytics-api,oauth2client,Python,Google Analytics,Openssl,Google Analytics Api,Oauth2client,我正在尝试连接到服务器应用程序中的Google Analytics,请按照以下说明进行操作:。本页建议使用P12键,但出于业务原因,我需要使用JSON键(在开发者控制台的密钥生成页上也是“推荐的”) 当我使用P12文件时,代码示例对我来说效果很好,authorize和build调用都可以工作,然后我就可以正确使用API了。我不能使用JSON文件。下面是一个简单的例子: from apiclient.discovery import build from oauth2client.client i

我正在尝试连接到服务器应用程序中的Google Analytics,请按照以下说明进行操作:。本页建议使用P12键,但出于业务原因,我需要使用JSON键(在开发者控制台的密钥生成页上也是“推荐的”)

当我使用P12文件时,代码示例对我来说效果很好,
authorize
build
调用都可以工作,然后我就可以正确使用API了。我不能使用JSON文件。下面是一个简单的例子:

from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
import json
import base64
import httplib2

def authorize(ga_email, ga_secret):
    jwt = SignedJwtAssertionCredentials(ga_email, ga_secret, scope=self.ga_scope, private_key_password='notasecret')
    http = jwt.authorize(httplib2.Http())
    return build('analytics', 'v3', http=http)

# this works
ga_email = 'random@words.gserviceaccount.com'
with open('client_secrets.p12', 'rb') as f:
    ga_secret = f.read()
service = authorize(ga_email, ga_secret)

# this fails
with open('client_secrets.json', 'r') as f:
    json_data = json.load(f)
ga_email = json_data['client_email']
ga_secret_json = json_data['private_key']
ga_secret_b64 = ''.join(ga_secret_json.split('\n')[1:-2])
ga_secret_bin = base64.b64decode(ga_secret_b64)
ga_secret = ga_secret_bin # or ga_secret_b64 or ga_secret_json
service = authorize(ga_email, ga_secret)
JSON尝试失败,出现以下错误(使用ga_secret_bin):

或者类似地(使用ga_secret_json或ga_secret_b64):

我尝试过其他几种排列方式:字符串处理和base64解码的变体,不使用
private\u key\u password
arg等等。我还尝试使用
from_json
,在创建了一个带有P12键的credentials对象之后,根据
to_json
的输出填充email、key和其他字段


我想我遗漏了一些简单的东西,但我对OpenSSL不太熟悉,所以我不知道该找什么。

我的解决方案:从1.0rc1升级google api python客户端

File "/usr/local/lib/python2.7/dist-packages/pyOpenSSL-0.15.1-py2.7.egg/OpenSSL/_util.py", line 48, in exception_from_error_queue
    raise exception_type(errors)
Error: [('asn1 encoding routines', 'ASN1_CHECK_TLEN', 'wrong tag'), ('asn1 encoding routines', 'ASN1_TEMPLATE_EX_D2I', 'nested asn1 error'), ('asn1 encoding routines', 'ASN1_TEMPLATE_NOEXP_D2I', 'nested asn1 error')]
Error: [('asn1 encoding routines', 'ASN1_CHECK_TLEN', 'wrong tag'), ('asn1 encoding routines', 'ASN1_ITEM_EX_D2I', 'nested asn1 error')]