2

I am having trouble getting App Links working with Parse.

Since my App is mobile only i wanted to use Facebook's Mobile Hosting API. And since you need to send your Facebook App Secret with the request i wanted to do it with Parse Cloud Code.

All i coud find on the Facebook documentation was how to do it with 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,   }'

so this is what i came up with in cloud code

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,}'
  }

the response i get is: Request failed with response code 400

now i just read that multipart/form-data is not supported withParse.Cloud.httpRequest so is there another way to do this?

update: just found out that you can send multipart data with a Buffer, so this is my code now

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
}

however i am still getting the same result :(

update2: got it working with content type application/x-www-form-urlencoded and normal body. But i think the error was somewhere in my parameters since i tested it with curl and got the same response

natario
  • 24,954
  • 17
  • 88
  • 158
user1171773
  • 113
  • 1
  • 1
  • 8

1 Answers1

1

It took me a few hours, but I finally got it working:

// 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);
    })
});
natario
  • 24,954
  • 17
  • 88
  • 158