2017-10-17 66 views
1

我試圖檢索,然後POST一個JPEG圖像到Foursquare的https://api.foursquare.com/v2/photos/add端點使用Axios在節點。我試着愛可信(和郵差)的一些方法,但總是收到Missing file upload同樣的錯誤響應:檢索然後POST照片到Foursquare與Axios簽入

{ 
    "meta": { 
    "code": 400, 
    "errorType": "other", 
    "errorDetail": "Missing file upload", 
    "requestId": "NNNNNNNNNNNNNNNNNNNNN" // not the true requestId 
    }, 
    "notifications": [ 
    { 
     "type": "notificationTray", 
     "item": { 
     "unreadCount": 0 
     } 
    } 
    ], 
    "response": {} 
} 

的圖像是使用谷歌靜態地圖API創建,檢索與愛可信GET要求:

const image = await axios.get(imageURL, { 
    responseType: "arraybuffer" 
}); 

這是包裝在async函數併成功返回緩衝區。的數據被讀入一個Buffer並轉換爲字符串:

const imageData = new Buffer(image, "binary").toString(); 

Here's an example imageData string。我也嘗試將字符串轉換爲base64

的字符串是POST版的Foursquare的端點:

const postPhoto = await axios.post(
    "https://developer.foursquare.com/docs/api/photos/add? 
    checkinId=1234& 
    oauth_token=[TOKEN]& 
    v=YYYYMMDD", 
    imageData, 
    { 
    headers: { "Content-Type": "image/jpeg" } 
    } 
); 

其中checkinIdoauth_tokenv PARAMS都是有效的。

我已經嘗試了不同的Content-Type值,base64編碼imageData和其他幾個解決方案在論壇和在這裏發現SO(大多數是幾歲),但沒有任何作品。響應errorDetail總是表示Missing file upload

問題可能在於如何構造POST請求,但我也可能錯誤地請求/處理圖像數據。第二(或第三或第四)眼睛檢查我把這一起會是超級有用。

回答

1

哎,我終於解決了這個問題。

我終於能夠通過郵遞員提供一些提示來工作。下面是使用request郵遞員代碼片段:

var fs = require("fs"); 
var request = require("request"); 

var options = { method: 'POST', 
    url: 'https://api.foursquare.com/v2/photos/add', 
    qs: 
    { checkinId: [MY CHECKING ID], 
    public: '1', 
    oauth_token: [MY OAUTH TOKEN], 
    v: [MY VERSION] }, 
    headers: 
    { 'postman-token': '8ce14473-b457-7f1a-eae2-ba384e99b983', 
    'cache-control': 'no-cache', 
    'content-type': 'multipart/form-data; boundary=---- WebKitFormBoundary7MA4YWxkTrZu0gW' }, 
    formData: 
    { file: 
     { value: 'fs.createReadStream("testimage.jpg")', 
     options: { 
      filename: 'testimage.jpg', 
      contentType: null 
     } 
     } 
    } 
    }; 

request(options, function (error, response, body) { 
    if (error) throw new Error(error); 

    console.log(body); 
}); 

這樣做的關鍵部分是fs.createReadStream()。我之前缺少的部分是將圖像作爲流傳遞給請求。

使用這個我能找出Axios公司要求:

const axios = require("axios"); 
const querystring = require("qs"); 
const FormData = require("form-data"); 

const getImageStream = async function(url) { 
    return await axios 
    .get(url, { 
     responseType: "stream" 
    }) 
    .then(response => response.data); 
}; 

let form = new FormData(); 
form.append("file", getImageStream([IMAGE URL])); 

const requestURL = "https://api.foursquare.com/v2/photos/add"; 
const requestParams = { 
    checkinId: [MY CHECKIN ID], 
    public: 1, 
    oauth_token: [MY OAUTH TOKEN], 
    v: [MY VERSION] 
}; 
const requestConfig = { 
    headers: form.getHeaders() 
}; 

try { 
    const postPhoto = await axios.post(
    requestURL + "?" + querystring.stringify(requestParams), 
    form, 
    requestConfig 
); 

    return postPhoto; 
} catch (e) { 
    console.error(e.response); 
} 

瞧,請求成功和圖像發佈到Foursquare的簽到。