为什么Google API 连接不上并且超时

问题描述 投票:0回答:1

我正在尝试使用 API 密钥,它确实在 Raspi 上拍摄了照片,但随后在尝试将其发送到云端硬盘时卡住了,我不确定为什么。

注意:出于隐私考虑,我拿出了钥匙和 ID,但实际上我确实拥有正确的信息

import os
import pickle
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from picamera import PiCamera
from time import sleep
from datetime import datetime

API_KEY = "key"
FOLDER_ID = "ID"

def authenticate_drive():
    service = build('drive', 'v3', developerKey=API_KEY)
    return service

def take_picture(service):
    now = datetime.now()
    date_string = now.strftime("%m_%d_%y_%H%M")
    file_path = os.path.join(os.path.dirname(__file__), 'camera', 'pics_new', 'pic%s.jpg' % date_string)
    
    with PiCamera() as camera:
        camera.resolution = (2592, 1944)
        camera.framerate = 20

        sleep(3)
        camera.capture(file_path)
        prPeriwinkle("TAKE PICTURE", datetime.now().strftime("%H:%M:%S"))
        
        prNeonPink("Sending to Drive", datetime.now().strftime("%H:%M:%S"))
        
        # Upload the picture to Google Drive
        file_metadata = {'name': os.path.basename(file_path), 'parents': [FOLDER_ID]}
        media = MediaFileUpload(file_path, mimetype='image/jpeg')
        file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()

        print("Uploaded to Google Drive")

def main():
    service = authenticate_drive()
    take_picture(service)

if __name__ == '__main__':
    main()

过了一会儿,它输出了这样的内容:
sock.connect((self.host, self.port)) socket.timeout: 超时

我已经尝试解决这个问题有一段时间了,但无法解决

我尝试使用 Google API 中的 API 密钥将图片上传到云端硬盘,但在发送图片时遇到困难。

python google-api google-drive-api raspberry-pi3
1个回答
0
投票

API 密钥只能让您访问公共数据。

您无法使用 API 密钥上传,它需要私人帐户到使用 oauth2 的用户帐户

© www.soinside.com 2019 - 2024. All rights reserved.