【问题标题】:Using Facebook's Mobile Hosting API with Parse Cloud Code for App Links使用 Facebook 的 Mobile Hosting API 和 Parse Cloud Code for App Links
【发布时间】:2015-08-11 23:44:52
【问题描述】:

我无法让 App Links 与 Parse 一起使用。

由于我的应用程序仅是移动应用程序,因此我想使用 Facebook 的移动托管 API。 并且由于您需要发送您的 Facebook 应用程序密钥以及我想使用 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

现在我刚刚读到 Parse.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 对其进行了测试并得到了相同的响应

【问题讨论】:

    标签: javascript facebook parse-platform applinks


    【解决方案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);
        })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 1970-01-01
      • 2013-05-05
      • 2014-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多