【问题标题】:Python and pushbullet api: send a filePython 和 pushbullet api:发送文件
【发布时间】:2015-03-07 17:43:19
【问题描述】:

我正在尝试使用 Pushbullet 在他们的 API docs 之后发送文件。 这是我的功能:

def push_file(AccessToken, file_name):
    f = open(file_name, 'rb')
    file_type = mimetypes.guess_type(file_name)[0]

    print("Uploading {0}...".format(file_name))
    try:
        data = {
            'file_name': file_name, 
            'file_type' : file_type
        }

        resp = requests.post(UPLOAD_REQUEST_URL, data=data, auth=(AccessToken, '')).json()
        if resp.get('error') != None:
            print("Error: {0}".format(resp.get('error')['message']))
            return

        file_url = resp.get('file_url')
        print(file_url)
        resp = requests.post(resp.get('upload_url'), data=resp.get('data'), auth=(AccessToken, ''), files={'file': f})

        data = { 
            'type' : 'file', 
            'file_name' : file_name, 
            'file_type' : file_type, 
            'file_url' : file_url, 
            'body' : ''
        }
        resp = requests.post(PUSH_URL, data=data, auth=(AccessToken, '')).json()


    except requests.exceptions.ConnectionError:
        traceback.print_exc()
    f.close()

但我不断得到:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='s3.amazonaws.com', port=443): Max retries exceeded with url: /pushbullet-uploads (Caused by <class 'ConnectionResetError'>: [Errno 104] Connection reset by peer)

如果我使用另一个 AccessToken,我仍然会收到此错误,即使这是我第一次发布到该网址。

【问题讨论】:

    标签: python python-requests pushbullet


    【解决方案1】:

    不幸的是,上传过程不是很好,希望很快会得到改进。这是一个不遵守 JSON 规则的请求。有一个 curl 示例显示了这一点(https://docs.pushbullet.com/#upload-request),但理解 curl 语法基本上是不可能的。

    这是我刚刚输入的一个示例,似乎可以正常工作:

    import requests
    import json
    
    ACCESS_TOKEN = '<your access token here>'
    resp = requests.post('https://api.pushbullet.com/v2/upload-request', data=json.dumps({'file_name': 'image.jpg'}), headers={'Authorization': 'Bearer ' + ACCESS_TOKEN, 'Content-Type': 'application/json'})
    if resp.status_code != 200:
        raise Exception('failed to request upload')
    r = resp.json()
    resp = requests.post(r['upload_url'], data=r['data'], files={'file': open('image.jpg', 'rb')})
    if resp.status_code != 204:
        raise Exception('failed to upload file')
    print r['file_name'], r['file_type'], r['file_url']
    

    【讨论】:

    【解决方案2】:

    根据 Pusbullet API

    所有 POST 请求都应通过 HTTPS 并使用 JSON 正文和 Content-Type 标头设置为“application/json”。

    尝试像这样更改您的 requests.post 调用:

    resp = requests.post(UPLOAD_REQUEST_URL, json=data, auth=(AccessToken, '')).json()
    

    使用json=data 而不是data=data。请求会自动将Content-Type 设置为application/json

    【讨论】:

    • 我不能使用json=data,我觉得我电脑上安装的requests版本太旧了。我也尝试手动设置 headers={'Content-type': 'application/json'} 但我一直得到相同的结果......
    • 尝试更新请求? pip install requests==2.5.3
    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多