如何使用服务帐户与谷歌的python api和驱动器?

2024-10-01 00:29:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试编写Python2.7脚本,将一个文件上传到我的个人googledrive文件夹中。在

几个问题之后我就知道了。这是我当前的错误:

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.

我已经尝试过运行这个命令了,比如这个questionanswer中的mentiod。在

openssl pkcs12 -in privatekey.p12 -nodes -nocerts > privatekey.pem
openssl pkcs8 -nocrypt -in privatekey.pem -passin pass:notasecret -topk8 -out pk.pem

我的privatekey.p12是从新的现代花哨的google开发者控制台下载的,原名为something-0123eed.json,看起来是这样的1

^{pr2}$

我的python代码如下所示:

#!/bin/env python2.7

from apiclient.discovery import build
from apiclient.http import MediaFileUpload
import httplib2
from oauth2client.client import SignedJwtAssertionCredentials


credentials = SignedJwtAssertionCredentials(
        service_account_name='0KsVeSAa91UtEGvY9lil@developer.gserviceaccount.com',
        private_key=key,
        scope = [
                'https://www.googleapis.com/auth/drive',
                'https://www.googleapis.com/auth/drive.file',
                'https://www.googleapis.com/auth/drive.appdata',
                'https://www.googleapis.com/auth/drive.apps.readonly'
        ]
)

http = httplib2.Http()
http = credentials.authorize(http)

drive_folder_id = 'jhIKHOG6supMhpjPJFHffZarwxP6'


service = build('drive', 'v2', http=http)


media_body = MediaFileUpload('/path/to/superfile.gpg'), mimetype='application/pgp-encrypted')
body = {
        'title': 'superfile.gpg',
        'description': '',
        'mimeType': 'application/pgp-encrypted',
        'parents': [{'id': drive_folder_id}]
}

file = service.files().insert(
        body=body,
        media_body=media_body).execute()

1:(当然,我用垃圾更改了值)


Tags: inhttpsimportcomauthhttpwwwservice
1条回答
网友
1楼 · 发布于 2024-10-01 00:29:37

我找到了answer in this gist

openssl pkcs12 -passin pass:notasecret -in privatekey.p12 -nocerts -passout pass:notasecret -out key.pem
openssl pkcs8 -nocrypt -in key.pem -passin pass:notasecret -topk8 -out privatekey.pem
rm key.pem

但在此之前,我必须重新生成一个新的privat密钥,但格式为P12。在

generate new P12 key

相关问题 更多 >