Wednesday, August 6, 2014

Facebook integration, App Link Hosting

So my goal today was to enable users to share their score on Facebook after each round.  It took me the entire day, but I did finally get it working.  I ran into quite a few problems and I couldn't find any answers online, so, as always, I hope this post can help someone out.

After importing the code with the help of the Getting Started guide, I jumped straight to the Sharing section.  This part of the guide is quite good, and within a few minutes I was able to share a post to my timeline.  And after looking through the documentation for a few minutes, I found that Facebook provides an App Link Hosting API, so you can link back to your app from Facebook.  It's actually really cool; If you are on mobile and you already own the app, clicking the link immediately launches that app.  If you don't have the app installed or you are on an unsupported platform, it launches Google Play to give you a chance to download it.  Unfortunately, the documentation for this feature is much less impressive than the Getting Started guide.  So it took a few hours of digging, Googling, and trial-and-error to figure out exactly what you need to do to set it up.  Looking back, all of the information you need can be found in the documentation, but it's quite spread out and it's not incredibly clear which steps are required.  But I digress...

Okay, so here are the steps to create a new App Link object:
  1. If you haven't yet, follow the Getting Started guide and link your app to a Facebook App.  Under the Settings section, make sure you fill out Package Name and Class Name, and turn on Single Sign On and Deep Linking.
  2. You need to find your App Token.  You can do this by pasting this URL in your browser (replace {app-id} and {app-secret} with the values from the Facebook app page): https://graph.facebook.com/oauth/access_token?client_id={app-id}&client_secret={app-secret}&grant_type=client_credentials
  3. To create the App Link object, you can use cURL from a terminal.  If you don't have cURL, you can use another method.  Replace the fields in this command with the appropriate values:
    {custom-uri} should follow typical URI structure.  You can do really complicated structures if you want, but in my case I only needed one to link to the main activity.  So for example, mine looked like: my-app-name://launch
  4. Take the ID from the response and use it to get the final link: https://fb.me/{app-link-id}  (If that link doesn't work, use the official method to get the URL)

I ran into a few errors while I was figuring all of this out.  So if you get any of these while trying to create the App Link, you might have made the same mistakes I did:


This one was due to using the wrong value for "access_token".  Before I read the access tokens documentation, I tried to use the "Client Token" value from the app page.  Clearly that doesn't work.

I then tried to use the Graph API Explorer to generate a token.  But that generates a User Access token, not an App Access token, so that didn't work either.


This one was due to copy-pasting the wrong App Access token.  Somehow I must have grabbed a sample token or something, because the app ID was 123456789000000, haha.  Not sure how I missed that one.

Anyway, that's about it.  Hopefully this helps someone at some point!

1 comment: