简体   繁体   English

如何在Google的python API和驱动器上使用服务帐户?

[英]How to use a service account with Google's python api and drive?

I try to write python 2.7 script to upload a file into my personal google drive folder. 我尝试编写python 2.7脚本将文件上传到我的个人Google驱动器文件夹中。

After several problems I stuck know. 经过几个问题,我坚持知道。 This is my current error: 这是我目前的错误:

NotImplementedError: PKCS12 format is not supported by the PyCrpto library. NotImplementedError:PyCrpto库不支持PKCS12格式。 Try converting to a "PEM" (openssl pkcs12 -in xxxxx.p12 -nodes -nocerts > privatekey.pem) or using PyOpenSSL if native code is an option. 如果可以选择使用本机代码,请尝试转换为“ PEM”(openssl pkcs12-在xxxxx.p12 -nodes -nocerts> privatekey.pem)或使用PyOpenSSL。

I already tried to run this commands, as mentiod in this question and answer . 我已经尝试运行此命令,在本mentiod 问题答案

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 this 1 : 从新的现代google开发人员控制台下载的我的privatekey.p12最初名为something-0123eed.json ,看起来像这样1

{
  "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: 我的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: (of course, I changed the values with junk) 1 :(当然,我更改了垃圾值)

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. 但是在此之前,我不得不重新生成一个新的P12格式的专用密钥。

生成新的P12密钥

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google云端硬盘api和服务帐户 - Google drive api and service account 使用服务帐户密钥的 Google Drive Python API 授权 - Google Drive Python API authorization using service account key Google Drive API服务帐户“用户已超过其Drive存储配额” python - Google Drive API service Account “The user has exceeded their Drive storage quota” python 使用oauth2服务帐户在python中对Google API进行身份验证 - Use oauth2 service account to authenticate to Google API in python Google Drive API 服务帐户身份验证以使用 Python 上传图像文件并将其插入到复制的文档中 - Google Drive API Service Account authentication to upload and insert an image file to into copied doc using Python Google Drive API 使用 python 中的服务帐户未显示与我共享的文件夹 - Google Drive API using Service Account in python not showing up folders that are shared to me 如何使用 Google Drive API 通过服务帐户访问域中的共享文件? - How to get access to shared files in a domain with a service account using the Google Drive API? 如何查找 Google 服务帐户拥有的文件列表? (已超出用户的 Drive 存储配额) - How to find the list of files owned by Google service account? (The user's Drive storage quota has been exceeded) 我可以使用 Python 服务帐户创建 Google Drive 文件夹吗? - Can I create a Google Drive folder using service account with Python? 如何将alt = media与Google Drive Python API结合使用? - How to use alt=media with Google Drive Python API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM