是什么让在'r'模式下解析文本文件比在'rb'模式下解析它更方便?特别是当有问题的文本文件可能包含非ASCII字符时.
关于appengine的SignedJwtAssertionCredentials(使用pycrypto 2.6)不支持PKCS12格式,因此我正在尝试使用PEM密钥,正如所建议的那样.
这是我的代码:
f = file(os.path.join(os.path.dirname(__file__), KEY_FILE), "r")
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
scope="https://www.googleapis.com/auth/drive"
http = httplib2.Http()
http = credentials.authorize(http)
Run Code Online (Sandbox Code Playgroud)
并且KEY_FILE
是一个PEM键,使用以下命令转换:
openssl pkcs12 -in privatekey.p12 -nodes -nocerts > privatekey.pem
Run Code Online (Sandbox Code Playgroud)
但我仍然得到这个错误,好像它没有认识到这是一个PEM密钥:
NotImplementedError: PKCS12 format is not supported by the PyCrpto library.
Try converting to a "PEM" (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option.
Run Code Online (Sandbox Code Playgroud)
如果我只将文件名传递给构造函数(不读取文件的内容),则会出现同样的错误
任何的想法?
我一直试图让服务帐户身份验证为Google Admin SDK工作几天,但无济于事.我正在使用谷歌新安装的google-api-python-client-1.2库.
我一直在关注该主题的Google文档.链接在这里:
HTPS://developers.google.com/accounts/docs/OAuth2ServiceAccount
HTPS://developers.google.com/api-client-library/python/guide/aaa_oauth
HTP://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.SignedJwtAssertionCredentials-class.html
并让tasks.py服务帐户示例正常工作,您可以在此处找到:
HTP://code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py R = c21573904a2df1334d13b4380f63463c94c8d0e8
并且一直在密切研究这两个相关主题的Stack Overflow线程:
google admin sdk目录api 403 python
使用Oauth2作为服务帐户(教育版)的Google管理员API - 403错误
并研究了gam.py(Dito GAM)中的相关代码.
然而,我仍然遗漏了一些东西,因为我在几乎每个编写的测试用例中都收到了'oauth2client.client.AccessTokenRefreshError:access_denied'异常.
以下是测试身份验证的简明示例:
import httplib2
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials
f = file('myKey.p12', 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(
'myServiceAdmin@developer.gserviceaccount.com',
key,
sub='myAdminUser@my.googleDomain.edu',
scope = ['https://www.googleapis.com/auth/admin.directory.user',])
http = httplib2.Http()
http = credentials.authorize(http)
service = build('admin', 'directory_v1', http=http)
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,我得到了这个堆栈转储和异常:
Traceback (most recent call last):
File "./test.py", line 17, in <module>
service = build('admin', 'directory_v1', …
Run Code Online (Sandbox Code Playgroud) file-io ×1
oauth ×1
openssl ×1
pem ×1
pkcs#12 ×1
pycrypto ×1
python ×1
python-2.7 ×1
text-parsing ×1