调用照片 API 时出错 [英] Error calling Pho.to API

查看:50
本文介绍了调用照片 API 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用 Pho.to API 来编辑照片,但每次尝试 POST 时都会遇到相同的错误.我已经两次和三次检查了我的 app_id 和我的 key,但我无法弄清楚我做错了什么.我目前正在使用 ARC Chrome 扩展来调用这个 api,所以我什至还没有开始编写那部分代码,我只是想从 api 得到一个真正的响应,以确保它甚至可以工作.

我尽可能地遵循了他们文档中的说明.这是供参考的链接:

之后:

I am trying to call the Pho.to API to edit photos, and every time I try to POST I get the same error. I've double and triple checked my app_id and my key, and I can't figure out what I'm doing wrong. I'm currently using the ARC Chrome Extension to call this api, so I haven't even started coding that part yet, I'm just trying to get a real response back from the api to make sure it even works.

I followed the instructions in their docs as well as I could. Here's the link for reference: http://developers.pho.to/documentation/sending-requests

Here is my api call:

http://opeapi.ws.pho.to/addtask?APP_ID=<my-app-id>&KEY=<my-key>&SIGN_DATA=910ceb5bdb238b9248a34cce8b29ba64d5f239df

And here is the response I get back (don't be deceived by the 200):

Status: 200 OK

<?xml version="1.0" ?>
<image_process_response>
    <status>SecurityError</status>
    <err_code>614</err_code>
    <description>Error in POST parameters: one or more parameters (DATA , SIGN_DATA or APP_ID) are empty</description>
</image_process_response>

Here's the PHP code I use to create the SHA1 for SIGN_DATA:

<?php
    echo hash_hmac('SHA1', '<image_process_call><lang>en</lang><image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url><methods_list><method order="1"><name>desaturation</name></method><method order="2"><name>cartoon</name><params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params></method></methods_list><result_format>png</result_format><result_size>600</result_size></image_process_call>','<my-key>');
?>

Here is the xml from above, formatted for readability:

<image_process_call>
    <lang>en</lang>
    <image_url order="1">http://www.w3schools.com/html/pic_mountain.jpg</image_url>
    <methods_list>
        <method order="1">
            <name>desaturation</name>
        </method>
        <method order="2">
            <name>cartoon</name>
            <params>fill_solid_color=1;target_color=(255,255,255);border_strength=20;border_width=3</params>
        </method>
    </methods_list>
    <result_format>png</result_format>
    <result_size>600</result_size>
</image_process_call>

Any help would be appreciated. Thanks in advance!

解决方案

So I figured out what was wrong. Here is my detailed solution for anyone else that may run into similar problems with this api (regardless of platform):

Part of the problem (as @u_mulder pointed out) is that DATA needs to be sent up along with SIGNED_DATA so that the SHA1 can be decoded on the other end.

The other piece that fixed my problem was removing <lang>en</lang>. For whatever reason, that was returning Error 613: Invalid SIGN_DATA parameter. English is the default language anyway, so it was unnecessary.

So after fixing those things, here's my final url:

http://opeapi.ws.pho.to/addtask/?app_id=<my-app-id>&key=<my-key>9&sign_data=e456c393d11797c1a2945a85dd49ba2208cc66de&data=%3Cimage_process_call%3E%3Cimage_url+order%3D%221%22%3Ehttp%3A%2F%2Fwww.heroesandheartbreakers.com%2Fimages%2Fstories%2Fblogarticles%2F2016%2FJanuary2016%2FTV-Recap-Arrow-4x11-Olicity-is-home-470.jpg%3C%2Fimage_url%3E%3Cmethods_list%3E%3Cmethod+order%3D%221%22%3E%3Cname%3Ecartoon%3C%2Fname%3E%3Cparams%3Efill_solid_color%3D1%3Btarget_color%3D%28255%2C255%2C255%29%3Bborder_strength%3D20%3Bborder_width%3D1%3C%2Fparams%3E%3C%2Fmethod%3E%3C%2Fmethods_list%3E%3Cresult_format%3Epng%3C%2Fresult_format%3E%3Cresult_size%3E1500%3C%2Fresult_size%3E%3C%2Fimage_process_call%3E

Notice that the url is encoded. This may or may not be necessary, I just encoded it to be safe.

This returns:

<?xml version="1.0" ?>
<image_process_response>
    <request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
    <status>OK</status>
    <description />
    <err_code>0</err_code>
</image_process_response>

And I can now use request_id to get the url of the edited image:

http://opeapi.ws.pho.to/getresult?request_id=010afc13-6bba-44dd-b278-4f3bd1e41946

Which returns the following xml:

<image_process_response>
    <request_id>010afc13-6bba-44dd-b278-4f3bd1e41946</request_id>
    <status>OK</status>
    <result_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url>
    <result_url_alt>http://worker-images.ws.pho.to.s3.amazonaws.com/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</result_url_alt>
    <nowm_image_url>http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png</nowm_image_url>
</image_process_response>

So the url of the final edited image is http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png (I believe links expire after 24 hours)

And we're done!

If you'd like to check out how I implemented this api in a simple Android app, here's the github link: https://github.com/youravgjoe/ColoringPageGenerator

Before:

After:

这篇关于调用照片 API 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆