Foursquare在通过api上传照片时出现文件上传失败/照片格式无效的错误[英] Foursquare missing file upload / InvalidPhotoFormat error while uploading photo through api

本文是小编为大家收集整理的关于Foursquare在通过api上传照片时出现文件上传失败/照片格式无效的错误的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在尝试使用API​​: https://api .

                    var accessToken = "myAccessToken";
                    var platformProfileId = "4squarePageId";
                    var b64content = "somebase64stringrepresentationofimage";
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId, 'photo': b64content};
                    request({
                        url: url, //URL to hit
                        form: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                    }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    });

我正在得到以下响应:

{ meta: 
   { code: 400,
     errorType: 'other',
     errorDetail: 'Missing file upload',
     requestId: '561fe6c1498e097824456e38' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

有人可以告诉我我在哪里做错了吗?

更新:

                             var queryObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId};
                             request({
                                url: url, //URL to hit
                                qs: queryObj, //query obj
                                method: 'POST',
                                headers: { 'Content-Type': 'image/jpeg' },
                                body: b64content
                            }, function(error, response, body){
                                if(error) {
                                    console.log(error);
                                    return cb(error);
                                } else {
                                    if(typeof body != 'object') {
                                        body = JSON.parse(body);
                                    }
                                    console.log(body);
                                    if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                        return callback_inner("error");
                                    }
                                    var mediaIdStr = body['response']['id'];
                                    return callback_inner(null, mediaIdStr);
                                }
                            });

尝试将图像作为后消息主体发送,但即使那样行不通.

更新2:

                var  b64mediaFilesArr = results.C;
                async.map(b64mediaFilesArr, function(b64content, callback_inner){
                    var imagename = new Date() + '.jpg';
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {
                        'oauth_token': accessToken, 
                        'v': '20151009', 
                        'pageId': platformProfileId, 
                        'photo': {
                            value: b64content,
                            options: {
                                filename: imagename,
                                contentType: 'image/jpeg'
                            }
                        }
                    };
                    request({
                        url: url, //URL to hit
                        formData: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                        }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    }); 

如果我使用上述代码,则响应有更改:

{ meta: 
   { code: 400,
     errorType: 'param_error',
     errorDetail: 'InvalidPhotoFormat: Unable to determine photo type',
     requestId: '56207798498ee45703ab6059' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

之后我会发疯.有人可以帮我吗?

解决方案

除了以下可接受的答案外,我还解决了Base64编码问题.对于使用Base64编码图像数据的人,您需要将图像的原始二进制代表发送到Foursquare.因此,这个答案帮助我做到了. 转换binary.tostring('encode64')

推荐答案

photo参数不存在. photo是响应字段.

图像数据是在HTTP请求上作为发布消息主体发送的.

编辑

您使用request?请参阅 -multipart-form-uploads

您不需要编码为base64.

其他推荐答案

这些是对我有用的请求选项:

var options = {
    'url': 'https://api.foursquare.com/v2/photos/add',
    'qs': {
        'v': '20161001',
        'oauth_token': ACCESS_TOKEN,
        'venueId': VENUE_ID
    },
    'formData': {
        'file': {
            'value': RAW_IMAGE_BUFFER,
            'options': {
                'filename': 'topsecret.jpg',
                'contentType': 'image/jpg'
            }
        }
    },
    'json': true
};

然后致电:

request.post(options, function(error, response, body){})

本文地址:https://www.itbaoku.cn/post/2090874.html

问题描述

I'm trying to add a photo to foursquare page using api: https://api.foursquare.com/v2/photos/add and following node.js code:

                    var accessToken = "myAccessToken";
                    var platformProfileId = "4squarePageId";
                    var b64content = "somebase64stringrepresentationofimage";
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId, 'photo': b64content};
                    request({
                        url: url, //URL to hit
                        form: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                    }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    });

I'm getting following response:

{ meta: 
   { code: 400,
     errorType: 'other',
     errorDetail: 'Missing file upload',
     requestId: '561fe6c1498e097824456e38' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

Can anyone please tell me where am I doing wrong ?

Update:

                             var queryObj = {'oauth_token': accessToken, v: '20151009', 'pageId': platformProfileId};
                             request({
                                url: url, //URL to hit
                                qs: queryObj, //query obj
                                method: 'POST',
                                headers: { 'Content-Type': 'image/jpeg' },
                                body: b64content
                            }, function(error, response, body){
                                if(error) {
                                    console.log(error);
                                    return cb(error);
                                } else {
                                    if(typeof body != 'object') {
                                        body = JSON.parse(body);
                                    }
                                    console.log(body);
                                    if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                        return callback_inner("error");
                                    }
                                    var mediaIdStr = body['response']['id'];
                                    return callback_inner(null, mediaIdStr);
                                }
                            });

Tried sending image as post message body but even then it's not working.

Update 2:

                var  b64mediaFilesArr = results.C;
                async.map(b64mediaFilesArr, function(b64content, callback_inner){
                    var imagename = new Date() + '.jpg';
                    var url = "https://api.foursquare.com/v2/photos/add";
                    var formObj = {
                        'oauth_token': accessToken, 
                        'v': '20151009', 
                        'pageId': platformProfileId, 
                        'photo': {
                            value: b64content,
                            options: {
                                filename: imagename,
                                contentType: 'image/jpeg'
                            }
                        }
                    };
                    request({
                        url: url, //URL to hit
                        formData: formObj, //form data
                        method: 'POST',
                        headers: { 'Content-Type': 'image/jpeg' }
                        }, function(error, response, body){
                        if(error) {
                            console.log(error);
                            return cb(error);
                        } else {
                            if(typeof body != 'object') {
                                body = JSON.parse(body);
                            }
                            console.log(body);
                            if(('meta' in body) && ('code' in body['meta']) && (body['meta']['code'] != 200)) {
                                return callback_inner("error");
                            }
                            var mediaIdStr = body['response']['id'];
                            return callback_inner(null, mediaIdStr);
                        }
                    }); 

If I use above code, then there is change in the response:

{ meta: 
   { code: 400,
     errorType: 'param_error',
     errorDetail: 'InvalidPhotoFormat: Unable to determine photo type',
     requestId: '56207798498ee45703ab6059' },
  notifications: [ { type: 'notificationTray', item: [Object] } ],
  response: {} }

I'm going crazy after this. Can anyone please help me out ?

Solution

In addition to below accepted answer, I solved base64 encoded problem. For those of you using base64 encoded image data in your web app, you need to send original binary rep of image to Foursquare. This SO answer helped me to do that. Convert Binary.toString('encode64') back to Binary

推荐答案

photo parameter does not exist. photo is response field.

The image data is sent as the POST message body on the HTTP request.

EDIT

You use request? Refer to https://github.com/request/request#multipartform-data-multipart-form-uploads

You don't need encode into base64.

其他推荐答案

These are the request options that worked for me:

var options = {
    'url': 'https://api.foursquare.com/v2/photos/add',
    'qs': {
        'v': '20161001',
        'oauth_token': ACCESS_TOKEN,
        'venueId': VENUE_ID
    },
    'formData': {
        'file': {
            'value': RAW_IMAGE_BUFFER,
            'options': {
                'filename': 'topsecret.jpg',
                'contentType': 'image/jpg'
            }
        }
    },
    'json': true
};

Then just call:

request.post(options, function(error, response, body){})