使用Facebook的Mobile Hosting API和Parse Cloud Code for App Links

时间:2015-08-11 23:44:52

标签: javascript facebook parse-platform applinks

我无法让App Links使用Parse。

由于我的应用程序仅限移动设备,因此我想使用Facebook的移动主机API。 因为你需要发送你的Facebook App Secret我想要用Parse Cloud Code做的请求。

所有我在Facebook文档中找到的是如何使用cURL:

curl https://graph.facebook.com/app/app_link_hosts \
-F access_token="APP_ACCESS_TOKEN" \
-F name="iOS App Link Object Example" \
-F ios=' [
    {
      "url" : "sharesample://story/1234",
      "app_store_id" : 12345,
      "app_name" : "ShareSample",
    },   ]' \
-F web=' {
    "should_fallback" : false,   }'

所以这就是我在云代码中提出的

Parse.Cloud.httpRequest({
  method: 'POST',
  url: 'https://graph.facebook.com/app/app_link_hosts',
  headers: {
    'Content-Type': 'multipart/form-data'
  },
  body: {
    access_token : "APP_ACCESS_TOKEN",
    name : "iOS App Link Object Example",
    ios : '[{"url" : "sharesample://story/1234","app_store_id" : 12345,"app_name" : "ShareSample",},]',
    web : '{"should_fallback" : false,}'
  }

我得到的回答是:请求失败,回复代码为400

现在我刚刚读到了withParse.Cloud.httpRequest不支持multipart / form-data 那还有另一种方法吗?

更新:刚发现你可以用Buffer发送多部分数据, 所以现在这是我的代码

var Buffer = require('buffer').Buffer;    
var access_token = new Buffer('APP_ACCESS_TOKEN','utf8');
var name = new Buffer('iOS App Link Object Example','utf8');
var ios = new Buffer('[{"url" : "sharesample://story/1234","app_store_id" : 12345,"app_name" : "ShareSample",},]','utf8');
var web = new Buffer('{"should_fallback" : false,}','utf8');

var contentBuffer = Buffer.concat([access_token, name, ios, web]);

Parse.Cloud.httpRequest({
  url: 'https://graph.facebook.com/app/app_link_hosts',
  method: 'POST',
  headers: {
    'Content-Type': 'text/html; charset=utf-8'
  },
  body: contentBuffer
}

但是我仍然得到相同的结果:(

update2:使用内容类型application / x-www-form-urlencoded和普通正文。但我认为错误是在我的参数中的某处,因为我用curl测试它并得到相同的响应

1 个答案:

答案 0 :(得分:1)

我花了几个小时,但我终于开始工作了:

// Returns the canonical url, like https://fb.me/....
Parse.Cloud.define("createAppLink", function(request, response) {
    // see https://developers.facebook.com/docs/graph-api/reference/v2.5/app/app_link_hosts

    var storyId = request.params.storyId + ''; // param identifying a single "post"
    var appId = 'APP_ID';
    var appSec = 'APP_SECRET';
    var appToken = appId + '|' + appSec; // your app token

    Parse.Cloud.httpRequest({
        url: 'https://graph.facebook.com/app/app_link_hosts',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ // you need to stringify it
            access_token: appToken,
            name: 'LINK TO ' + storyId, // it is needed but not public
            android: [{
                url: 'app://story/' + storyId, // deep link url
                package: 'com.package', // your package name
                app_name: 'APP' // your app name
            }],
            web: { should_fallback: 'false' }
        })
    }).then(function(httpResponse) {
        // We get an id, by which we can fetch
        // the canonical url with a get request
        var data = JSON.parse(httpResponse.text);
        var id = data.id;
        return Parse.Cloud.httpRequest({
            url: 'https://graph.facebook.com/' + id,
            method: 'GET',
            headers: {
                'Content-Type': 'application/json'
            },
            params: {
                access_token: appToken,
                fields: 'canonical_url',
                pretty: 'true'
            }
        });
    }).then(function(httpResponse) {
        var data = JSON.parse(httpResponse.text);
        var canonicalUrl = data.canonical_url;
        response.success(canonicalUrl);

    }, function(error) {
        response.error(error);
    })
});