通过api上传照片时,Foursquare缺少文件上传/InvalidPhotoFormat错误 [英] Foursquare missing file upload / InvalidPhotoFormat error while uploading photo through api

查看:87
本文介绍了通过api上传照片时,Foursquare缺少文件上传/InvalidPhotoFormat错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用api将照片添加到Foursquare页面: https://api .foursquare.com/v2/photos/add 以及以下node.js代码:

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);
                        }
                    });

我得到以下回应:

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

更新:

                             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.

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

解决方案

除了下面接受的答案外,我还解决了base64编码问题.对于那些在Web应用程序中使用base64编码的图像数据的用户,您需要将图像的原始二进制表示发送到Foursquare.这样的回答帮助我做到了. 将Binary.toString('encode64')转换为Binary

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参数不存在. photo是响应字段.

photo parameter does not exist. photo is response field.

图像数据作为HTTP请求上的POST消息正文发送.

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

编辑

您使用request吗?请参阅 https://github.com/request/request#multipartform-data -multipart-form-uploads

您不需要编码为base64.

You don't need encode into base64.

这篇关于通过api上传照片时,Foursquare缺少文件上传/InvalidPhotoFormat错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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