Using Facebook's Mobile Hosting API for App Links

分类目录: Applinks

Using Facebook's Mobile Hosting API for App Links

If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links.

For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you. With the Hosting API you can create and manage your App Links for all the mobile environments you support.

This is the typical flow to configure an App Link for a piece of content:

  1. Create a new App Link object. You can set up an object for one platform or several at the same time.
  2. Save the App Link object ID that's returned.
  3. Use that ID to get the URL you can use to share the content.
  4. Configure additional platforms that you support.
  5. If supporting Android, configure your app's manifest file for the configured URLs.

These examples use Facebook's Graph API to post data. You can learn more about our Graph API in Facebook's documentation on the topic.

You can also use the web-based App Links tool to create an App Link URL.

Create App Link

iOS

You create a new App Link object for a content targeted to iOS like this:

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

A successful call returns an ID that represents the App Link object hosted on Facebook, for example:

{"id":"643402985734254"}

You can retrieve the canonical URL that points to your new App Link object through this API call:

curl -G https://graph.facebook.com/YOUR_APP_LINK_HOST_ID \
-d access_token="APP_ACCESS_TOKEN" \
-d fields=canonical_url \
-d pretty=true

Where YOUR_APP_LINK_HOST_ID represents the id returned from creating your App Link object. Your response will look like this:

{
   "canonical_url": "https://fb.me/643402985734254",
   "id": "643402985734254"
}

See the App Links Hosting API for non-cURL examples.

Android

You create a new App Link object for a content targeted to Android like this:

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

A successful call returns an ID that represents the App Link object hosted on Facebook, for example:

{"id":"643402985734299"}

You can retrieve the canonical URL that points to your new App Link object through this API call:

curl -G https://graph.facebook.com/YOUR_APP_LINK_HOST_ID \
-d access_token="APP_ACCESS_TOKEN" \
-d fields=canonical_url \
-d pretty=true

Where YOUR_APP_LINK_HOST_ID represents the id returned from creating your App Link object. Your response will look like this:

{
   "canonical_url": "https://fb.me/643402985734299",
   "id": "643402985734299"
}

The previous example showed you how to create a new App Link object. You add configuration data to an existing App Link object for a content targeted to Android like this:

curl https://graph.facebook.com/643402985734254 \
-F access_token="APP_ACCESS_TOKEN" \
-F android=' [
    {
      "url" : "sharesample://story/1234",
      "package" : "com.facebook.samples.sharesample",
      "app_name" : "ShareSample",
    },
  ]'

The example shows you modifying an existing App Link object represented by the Graph API path /643402985734254 to add the Android-specific information.

Support sharesample URLs in Android

When a shared link is tapped your Android app is launched through the URL specified in the App Link object you just created (if you didn't specify a url, then it will use the canonical URL – i.e. the https://fb.me/xxxxx URL). To set up your app to respond to this URL, you need to add an intent filter in your app's manifest file. The filter should respond to the sharesample scheme (if you didn't specify a URL, then your filter should respond to the fb.me host and https scheme), handle implicit intents, and accept the ACTION_VIEW action. The example below adds an intent filter to the MainActivity that handles this:

<activity android:name=".MainActivity"
    android:label="@string/app_name" >
    ...
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="sharesample" />
    </intent-filter>
</activity>

Once you've set up your web pages based App Links, you're ready to handle incoming links to your app.

Setting Up App Links for Existing Content

If you have existing web content, then you can configure your App Links by adding metadata to your web pages.