【问题标题】:Zamzar API download failedZamzar API 下载失败
【发布时间】:2016-04-20 12:45:05
【问题描述】:

无法使用 python 程序从 zamzar api 下载转换后的文件,正如 https://developers.zamzar.com/docs 中指定的那样,但因为我正确地使用了代码和 api 密钥。它只显示错误代码:20。在这个错误后面浪费了 4 小时,请有人。

import requests
from requests.auth import HTTPBasicAuth

file_id =291320
local_filename = 'afzal.txt'
api_key = 'my_key_of_zamzar_api'
endpoint = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id)

response = requests.get(endpoint, stream=True, auth=HTTPBasicAuth(api_key, ''))

try:
  with open(local_filename, 'wb') as f:
    for chunk in response.iter_content(chunk_size=1024):
      if chunk:
        f.write(chunk)
        f.flush()

    print("File downloaded")

except IOError:
  print("Error")

这是我用来下载转换文件的代码。

【问题讨论】:

  • 请复制完整的输出/回溯。

标签: python api request


【解决方案1】:

这段代码很容易将文件转换成不同的格式:

import requests
from requests.auth import HTTPBasicAuth
#--------------------------------------------------------------------------#
api_key = 'Put_Your_API_KEY' #your Api_key from developer.zamzar.com
source_file = "tmp/armash.pdf" #source_file_path
target_file = "results/armash.txt" #target_file_path_and_name
target_format = "txt"  #targeted Format.
#-------------------------------------------------------------------------#



def check(job_id,api_key):
    check_endpoint = "https://sandbox.zamzar.com/v1/jobs/{}".format(job_id)
    response = requests.get(check_endpoint, auth=HTTPBasicAuth(api_key, ''))
    #print(response.json())
    #print(response.json())
    checked_data=response.json()
    value_list=checked_data['target_files']
    #print(value_list[0]['id'])
    return value_list[0]['id']

def download(file_id,api_key,local_filename):
    downlaod_endpoint = "https://sandbox.zamzar.com/v1/files/{}/content".format(file_id)
    download_response = requests.get(downlaod_endpoint, stream=True, auth=HTTPBasicAuth(api_key, ''))
    try:
      with open(local_filename, 'wb') as f:
        for chunk in download_response.iter_content(chunk_size=1024):
          if chunk:
            f.write(chunk)
            f.flush()

        print("File downloaded")

    except IOError:
      print("Error")


endpoint = "https://sandbox.zamzar.com/v1/jobs"
file_content = {'source_file': open(source_file, 'rb')}
data_content = {'target_format': target_format}
res = requests.post(endpoint, data=data_content, files=file_content, auth=HTTPBasicAuth(api_key, ''))
print(res.json())
data=res.json()
#print(data)
print("=========== Job ID ============\n\n")
print(data['id'])
target_id=check(data['id'],api_key)
print("\n================= target_id ===========\n\n")
print(target_id)
download(target_id,api_key,target_file)

希望这个好人!。

【讨论】:

    【解决方案2】:

    我是 Zamzar API 的首席开发人员。

    因此,Zamzar API 文档包含有关错误代码的部分(请参阅https://developers.zamzar.com/docs#section-Error_codes)。您的错误的相关代码是:

    { "message" : "API 密钥丢失或无效", “代码”:20 }

    这可能意味着或者您根本没有指定 API 密钥使用的 API 密钥对于您尝试下载的文件无效。似乎更可能是后者,因为您的代码包含一个 api_key 变量。

    查看您的代码,您可能已使用 job ID (291320) 来尝试下载文件,而实际上您应该使用 file ID .

    每个转换作业可以输出 1 个或多个转换后的文件,您需要为要抓取的文件指定文件 ID。您可以通过查询/jobs/ID 并查看target_files 数组来查看所有已转换文件ID 的列表。这在https://developers.zamzar.com/docs#section-Download_the_converted_file的 API 文档中进行了概述

    因此,如果您将代码更改为使用 Job 的 target_files 数组中的 文件 ID,您的下载应该会立即生效。

    很抱歉你在这件事上浪费了时间。很明显,如果它已达到 S.O.我们的文档在解释这种区别方面做得不够好,所以我们将看看我们可以做些什么来使它们更清楚。

    转换愉快!

    【讨论】:

    • 是的,我后来遇到了问题。在浪费了2个小时之后。下面我也发布了解决方案。我后来发现的。下面的答案是从转换文件到下载的完整解决方案脚本。看看吧!
    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 2016-03-28
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多