如何在Google的python api和驱动器上使用服务帐户? [英] How to use a service account with Google's python api and drive?
问题描述
我尝试编写python 2.7脚本将文件上传到我的个人谷歌驱动器文件夹。
在我遇到几个问题后,我就知道了。这是我当前的错误:
NotImplementedError:PyCrpto库不支持PKCS12格式。尝试转换为PEM(openssl pkcs12 -in xxxxx.p12 -nodes -nocerts> privatekey.pem)或使用PyOpenSSL(如果本机代码是一个选项)。
我已经试着运行这个命令,就像这篇问题中的mentiod一样,answer 。
openssl pkcs12 -in privatekey .p12 -nodes -nocerts> privatekey.pem
openssl pkcs8 -nocrypt -in privatekey.pem -passpass pass:notasecret -topk8 -out pk.pem
从新的现代Google开发者控制台下载的privatekey.p12是原始的 something-0123eed.json
,看起来像这样 1 < :
$ b
{
private_key_id:9ced108fe72345373b75b03d7e967a3f8c0084ca,
private_key: ----- BEGIN PRIVATE KEY ----- \\\
xe91Tr6RHs57LKX2HivFmOQwcFoJkUPrbB6Gwy8prE ... Pc9jNExo5Krp1kLrkJYxAOmUWxWwPJ4pCx7Lxc6uQQnAlKyRmnfVpdS2I0\\\
----- END PRIVATE KEY ----- \\\
client_email: 0KsVeSAa91UtEGvY9lil@developer.gserviceaccount.com,
client_id:0KsVeSAa91UtEGvY9lil.apps.googleusercontent.com,
type:service_account
}
我的Python代码如下所示:
#!/ bin / env python2.7
from apiclient.discovery import build $ b $ from apiclient.http import MediaFil eUpload
从oauth2client.client导入httplib2
import SignedJwtAssertionCredentials
$ b 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}]
} $ (
$ body $ body $ b $ media_body = pre>
<1> :(当然,我用垃圾改变了这些值)
解决方案我在这个要点找到了答案:
openssl pkcs12 -passpass pass:notasecret -in privatekey.p12 -nocerts -passout pass:notasecret -out key.pem
openssl pkcs8 -nocrypt -in key.pem -passpass pass:notasecret -topk8 -out privatekey.pem
rm key.pem
但在此之前,我必须重新生成一个新的私钥,但采用P12格式。
$ b
I try to write python 2.7 script to upload a file into my personal google drive folder.
After several problems I stuck know. This is my current error:
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.
I already tried to run this commands, as mentiod in this question and answer.
openssl pkcs12 -in privatekey.p12 -nodes -nocerts > privatekey.pem
openssl pkcs8 -nocrypt -in privatekey.pem -passin pass:notasecret -topk8 -out pk.pem
my privatekey.p12 downloaded from the new modern fancy google developers console was original named something-0123eed.json
and looked like this1:
{
"private_key_id": "9ced108fe72345373b75b03d7e967a3f8c0084ca",
"private_key": "-----BEGIN PRIVATE KEY-----\nxe91Tr6RHs57LKX2HivFmOQwcFoJkUPrbB6Gwy8prE...Pc9jNExo5Krp1kLrkJYxAOmUWxWwPJ4pCx7Lxc6uQQnAlKyRmnfVpdS2I0\n-----END PRIVATE KEY-----\n",
"client_email": "0KsVeSAa91UtEGvY9lil@developer.gserviceaccount.com",
"client_id": "0KsVeSAa91UtEGvY9lil.apps.googleusercontent.com",
"type": "service_account"
}
My python code looks like this:
#!/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: (of course, I changed the values with junk)
解决方案 I found the 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
But before this, I had to regenerate a new privat key but in P12 format.
这篇关于如何在Google的python api和驱动器上使用服务帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!