2016-12-01 85 views
2

我想調用Pho.to API來編輯照片,並且每次嘗試POST時都會得到相同的錯誤。我已經雙倍和三倍檢查了我的app_id和我的key,我無法弄清楚我做錯了什麼。我目前使用ARC Chrome擴展來調用這個api,所以我甚至還沒有開始編寫這個部分,我只是試圖從api得到一個真正的迴應,以確保它的工作。錯誤調用Pho.to API

我遵循他們的文檔中的說明以及我可以。這裏的鏈接以供參考:http://developers.pho.to/documentation/sending-requests

這裏是我的API調用:

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

,這裏是響應我回去(不要被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> 

下面是我用來創建SHA1的PHP代碼: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>'); 
?> 

這裏是從上面的xml,格式化爲可讀性:

<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> 

任何幫助,將不勝感激。提前致謝!

+0

您不應該同時發送'DATA'和'SIGN_DATA'嗎?由於'hmac'返回一個散列,從哪個API不能恢復你傳遞的原始xml內容。 –

+0

@u_mulder我試圖與有效載荷的其餘部分一起發送上行數據,現在我得到這個錯誤: '的SecurityError 無效SIGN_DATA參數' – youravgjoe

+0

@u_mulder那修復了部分問題。我還刪除了' en',現在API已經過了。謝謝您的幫助! – youravgjoe

回答

3

所以我想通了什麼是錯的。這是我對任何人都可以與此API(平臺如何)遇到類似問題的詳細解決方案:

問題的

部分(如@u_mulder指出)是DATA需要與SIGNED_DATA這樣一起發送了SHA1可以在另一端解碼。

解決我的問題的另一部分是刪除<lang>en</lang>。無論出於何種原因,這是返回Error 613: Invalid SIGN_DATA parameter.無論如何英語是默認語言,所以它是沒有必要的。

所以解決了那些事以後,這是我的最終網址:

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 

注意,URL編碼。這可能或可能不必要,我只是編碼它是安全的。

這將返回:

<?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> 

而且我現在可以使用request_id來獲取編輯圖像的URL:

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

它返回下面的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> 

所以最終編輯圖片的網址是http://worker-images.ws.pho.to/i1/3BCB160A-691A-458B-9161-67AFA8A9EAA0.png(我相信鏈接會在24小時後過期)

我們完成了!

如果你想看看我是如何在一個簡單的Android應用程序來實現這個API,這裏的GitHub的鏈接:https://github.com/youravgjoe/ColoringPageGenerator

前:

Before

後:

After

0

對我來說,錯誤是由(A)確定使用SHA1而不是SHA256(我自己的錯誤)和(B)出於某種原因,sign_data值必須是小寫。