Hey Folks,
A couple of months ago I was wrestling with the Facebook API to put my Cat Word Poker game up on there, but this just so happened to be when Cambridge Analytica blew up in the news for getting Trump elected, causing idiots to vote for Brexit, and who knows what else – causing Facebook and Zuckerberg to be under the microscope. Apart from the sweet memes, this also caused Facebook app submissions to get suspended for an unknown period, so I hastily moved over to a Kongregate release until Facebook became available again.
Shown above: Sweet Memes
Now the gates are open again, I’ve come back to Facebook API only to find things bit broken for random reasons.
First up, my game was no longer detecting that it was running on Facebook, so was incorrectly logging in by deviceID (generated GUID on WebGL).
Possibly Facebook have changed their embedding strategy or user agent string, but I managed to fix it by using the referrer url:
[code language=”csharp”]
if (url.indexOf("apps.facebook.com") > -1)
{
onFacebook = true;
}
else
{
// try with referrer
if (document.referrer && document.referrer.indexOf("apps.facebook.com") > -1)
{
onFacebook = true; // This branch is needed now, as the one below doesn’t work anymore
}
else
{
var ua = navigator.userAgent || navigator.vendor || window.opera;
onFacebook = (ua.indexOf("FBAN") > -1) || (ua.indexOf("FBAV") > -1);
}
}
[/code]
Second up, my test accounts no longer spit out valid access tokens, causing PlayFab to get confused and show a broken login. I refreshed the access tokens but nothing was working. Google wasn’t helpful, so I tried creating a new test account from the interface and it worked fine. Classy. I nearly tested the payment with my real card before remembering you need to separately add test accounts as test ‘payment’ accounts, but that’s fine, that works.
I’m now at the stage where the payment starts correctly, but the response doesn’t parse properly.
This is a problem I’d encountered before the Zuckerberg trial, but thought I was just parsing things wrong somehow.
Well – I ‘was’ doing it wrong, but unfortunately by being misled.
Read the answer to this thread or this.
Turns out that the Facebook Unity plugin will return an example callback with data in a different format (different field names) to how it actually happens in the wild. Fantastic – I suppose I need to write different code for the editor and for reality, amazing job folks. I’m guessing the API changed somewhere along the line, and the Unity Facebook SDK didn’t keep pace? Who knows. I’d be much happier if the editor stubs were more accurate in other ways (like showed the purchase popup dialog flow), but serving the wrong data schema is just facepalm worthy.
Anyway, that was my morning – I’m leaving this blog entry open because I’m sure something else will trip me…
Ok that didn’t take long: Unable to cast object of type ‘Int64’ to type ‘String’.
The PlayFab code takes a string at the verification phase, and the Facebook dictionary returns an int64 for that field, so if I just change my code to do that conversion…
…hmm, another error because I need to store the orderID (waiting another 5 mins for WebGL build, FB upload, deploy, loading game in browser) and…
It works!
Well, I’m not sure the async purchase stuff works, but after some research it seems that PlayFab needs to get a client-side confirmation (even though the payment hasn’t finished) – having payments that don’t come back until potentially days later is weird 🙂
Anyway, nice! The rest should be details now.
Side rage: Why this:
I can set a fixed height but not a fixed width? Guess I’ll change my CSS >_<
Anyway, I’d say I’m rather Taken by the Unity Facebook API…
EDIT:
Forgot to mention, I figured out the async payments in the end – here’s my explanation of the correct order:
PlayFab forum
Basically you have to confirm the payment through PlayFab if Facebook comes back with “initiated” rather than just “completed” 🙂