GLBasic forum

Codesnippets => Inline / 3rd party => Topic started by: spicypixel on 2011-Jul-14

Title: Facebook connection
Post by: spicypixel on 2011-Jul-14
1) Go to http://www.facebook.com/developers (http://www.facebook.com/developers)
2) Click "+ Create New App" in the top right
3) Name the App accordingly, tick the terms and click "Continue"
4) Fill in the captcha
5) Enter a relevant name in the Canvas Page (http://apps.facebook.com/yourname/)
6) Enter your canvas url (http://www.yourdomain.com/folderwithzipcontents/
7) Click "Save Changes"
8) Click "Web" on the left navigation
9) For site url use "http://yourdomain.com"
10) For site domain use "yourdomain.com"
11) Copy your App ID and App Secret
12) Click "Save Changes"

Now it's simply a matter of editing the config.php file with the appid and appsecret and then uploading the contents of the zip archive to the folder defined in step 6. Accessing http://www.yourdomain.com/folderwithzipcontents/update_status.php will now do it's stuff.

It's very basic but it does work. Hope you can dabble and expand upon it :)

** Note **
To get your access token for your created app visit  http://www.facebook.com/developers (http://www.facebook.com/developers) again and click on your app from the left-hand side. You will now see the full details of your app and a code will be displayed in the Access Token field. If there is no access token there click the given link to grant permissions, a code will now be shown. This access token is needed in the new updated code attached.




[attachment deleted by admin]
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-14
Thanks spicy, really helpful. After the so many things I tried yesterday this was one of them. And i get this error again with your info. "Facebook needs the CURL PHP extension". Is it that my webhost doesn't support cUrl?

Thanks for help spicy, your step by step was very well explained. Now about this error, no idea what to do.
Title: Re: Facebook connection
Post by: Kitty Hello on 2011-Jul-14
You must install the curl extension. I found this script to test the things you need.

Code: GLBasic [Select]
    $curlcontent="curl";
    $curlstatus="Curl is not installed  - This is a Problem";
    if (function_exists('curl_init')) {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, 'https://api.facebook.com/restserver.php');
       curl_setopt($ch, CURLOPT_HEADER, 0);
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
       $curlcontent = curl_exec($ch);
       curl_close($ch);
       $curlstatus="Curl is available but cannot access Facebook - This is a problem ";
       if (strlen($curlcontent)>6) {$curlstatus="Curl is available and can access Facebook - All is OK";}
    }
 

Would there be a safe way to host such a script on GLBasic.com for _all_ users, but prevent it from being abused? I'd host it then for you all.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-15
You could host the facebook.php script Gernot which would then be referenced in the config.php this should work. I've now amended the update_status.php script too.

Code: GLBasic [Select]
<?php
$msge = $_POST['mymsg'];
$msge = scrub($msge);

require_once 'fbconfig.php';

$attachment =  array(
         'access_token' => "mytokenhere",
          'message' => $msge,
                'name' => "BHPool Player Management System",
                'link' => "http://bhpool.spicypixel.net",
                'description' => 'Posted from the BHPool Facebook App',
          'picture'=>"http://bhpool.spicypixel.net/image.jpg",
        );
$facebook->api('/me/feed', 'POST', $attachment);

header('Location: http://bhpool.spicypixel.net');

// .----------------------------------------.
// | Prevent XSS Hacks & Mail Injection     |
// '----------------------------------------'
function scrub($EXPLOIT) {
         // Pre-Scrub with HTMLENTITIES
         $EXPLOIT = htmlentities($EXPLOIT,ENT_QUOTES);
         // Mail Check for exploit
         $scrubbed = preg_replace("(\n|\r)", "<br />", $scrubbed);
         // Return Value
         return $scrubbed;
}
?>
 

In the example above I'm using my own details (access token) for a website I made hence the header('Location: http://bhpool.spicypixel.net'); command which jumps back to my website when the status is updated. I've used _POST to grab the message from a form but I could easily have used _GET which is more appropriate from an app.

Examples:
www.yourdomain.com/update_status.php?mymsg=HelloWorld
www.yourdomain.com/update_status.php?name=spicy&score=2
Title: Re: Facebook connection
Post by: Kitty Hello on 2011-Jul-15
ok. with the access token being an unique ID for the person/app that's posting, I can safely put this on my server?
Can you post your scrub function?
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-15
Thanks spicy, really helpful. After the so many things I tried yesterday this was one of them. And i get this error again with your info. "Facebook needs the CURL PHP extension". Is it that my webhost doesn't support cUrl?

Thanks for help spicy, your step by step was very well explained. Now about this error, no idea what to do.

It's your webhost not supporting cURL you may find that they only support v4 of php too not v5 not sure if that makes a difference but it might.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-15
I've just made a GLB Status Updater App in facebook which you can test out here (http://www.spicypixel.net/glbasic)
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-15
ok. with the access token being an unique ID for the person/app that's posting, I can safely put this on my server?
Can you post your scrub function?

Yes you only need to host the facebook.php file though similar to how people link to the google version of jquery. This should then allow (in theory) the users to host the other files on their own webhost safely. They would change the config.php file to reference your facebook.php file on your server. This would be completely safe.

The scrub function is in the code above however the facebook_update.zip I removed the function and performed the commands directly as it seemed faster when submitting to facebook and prevented null text from being submitted.
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-15
Thanks spicy for everything. LOLLL i was doing correctly the first time I tried, it was just my stupid webhost not supporting cUrl.
I tried a free webhost with cUrl pre-installed and voila, I post no problems. Now comes the other part. From glBasic can this be only called from NETWEBEND, and if so this ends the program, not what I really want. Can we work this with Netwebget$, or is there a way around using NETWEBEND without ending the app.

Thanks a lot for your help. Ill start some testing with netwebget, but I dont think the php script will run the browser.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-15
Yeh i was thinking about doing the GLBasic side of things next, see what I can come up with ;)
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-15
Go for it spicy. :)
I have been testing and with netwebend works perfectly but app exits. Now what Im after of course.

If only netwebend would have like control as in: App stays running but like static/paused, then when you do your FB thing, control goes back to your App.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-16
Go for it spicy. :)
I have been testing and with netwebend works perfectly but app exits. Now what Im after of course.

If only netwebend would have like control as in: App stays running but like static/paused, then when you do your FB thing, control goes back to your App.

Post your glb source bud so I can see what you're doing within glb. Not your app off course but the netwebend part
Title: Re: Facebook connection
Post by: Kitty Hello on 2011-Jul-16
netwebend on V10 won't end if you set ALLOWESCAPE FALSE. Or was it AUTOPAUSE TRUE - check both or the online maunal.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-16
netwebend on V10 won't end if you set ALLOWESCAPE FALSE. Or was it AUTOPAUSE TRUE - check both or the online maunal.

Excellent ;-)
Title: Re: Facebook connection
Post by: Moebius on 2011-Jul-16
This page:
www.facebook.com/dialog/oauth?client_id=APP_ID&redirect_uri=YOUR_URL&display=touch

is the one to go to for granting permissions  (see http://developers.facebook.com/docs/guides/mobile/#web), but unfortunately NETWEBEND isn't a, 'neat', way to do it...
Title: Re: Facebook connection
Post by: Moebius on 2011-Jul-16
Posted in some answers thread was a link to this php script:  http://pastie.org/619912 (http://pastie.org/619912)
It deviously logs in to facebook.  Perhaps a php script could read ENCRYPTed username, password, and appid values and handle login, permissions, etc. automatically?  Looks difficult though (emulating a browser requesting the pages....)
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-18
The code you posted Serpent is hard-coded with the login info. Using my script asks the user for their permissions to allow the app this is the correct way of doing things.
Title: Re: Facebook connection
Post by: Kitty Hello on 2011-Jul-18
So we have a script that's useable for all users here and has no security/privacy leaks?
Title: Re: Facebook connection
Post by: Moebius on 2011-Jul-18
Quote
The code you posted Serpent is hard-coded with the login info.
I meant if you wanted to avoid opening a web browser, you'd have to hardcode your own dodgy login + grant permissions script - not recommended nor easy...  :P
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-18
So we have a script that's useable for all users here and has no security/privacy leaks?

the facebook.php script is part of the facebook sdk and it already has checks for security within it so is safe to host. The other files will have to be hosted by each user as effectively they have unique app id's and such created within facebook.
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-20
Hows this going spicy?
Ok with Glbasic v10 I can pause program, run Netwebend "url" and post to facebook successfully. A big problem though, I can't return control to glbasic unless I click the actual screen. This im sure is not going to work on iOS, havent tried though. Been testing on PC only. Another problem is theres no interaction whether if post has been successful or not. Doing this with netwebend is very hard to control. I want to exit Facebook, return control to glbasic app together with a value to be read by GlBasic to verify if post to facebook has been successful. How to do this is whacking my brains,  :rant: and maybe its not even doable with Netwebend.
your feedback is greatly appreciated.
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-20
Damn. just as I thought. I compiled for Xcode. I can post but I stay in the Facebook page. I cant exit it and return to the app, even though it is running.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-21
Damn. just as I thought. I compiled for Xcode. I can post but I stay in the Facebook page. I cant exit it and return to the app, even though it is running.

Ive been busy with work but I'll look into ways to post via glb other than using netwebend
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-21
Tried again today. No Success. Netwebend will not work. I can post but cant restore control to my App in iOS.
Netwebget$ and Netwebget will definately not work as you need to run php script from the browser. Been trying for hours. Anyone would know what to do?
Title: Re: Facebook connection
Post by: Slydog on 2011-Jul-21
Well, an easy solution would be to only post when your app exits.
Just queue up the posts and post them all at the same time.
Of course this may not be ideal for everybody and every situation!

[Edit] Hmm, well I'm guessing you couldn't actually post more than once at the end, unless the GLB code is still running in the background after the first post to continue posting the remainder.
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-22
Thanks Sly. But that won't work for what I want. Firstly no one wants a Facebook login screen when exiting the app. Plus asking person if they want to post to facebook upon exit is not a good idea. And the most important crucial thing is I have intentions of giving something when user posts. Im happy he is happy. Thats the way to encourage. Now how Im I going to do that upon exit :(. I really don't know what to do. Really need some pro help here.

Anyone?   :'(
Title: Re: Facebook connection
Post by: Kitty Hello on 2011-Jul-22
well, you can still try to do it in php and just call that website to post a string automatically. No?
Title: Re: Facebook connection
Post by: spicypixel on 2011-Jul-22
There is a facebook iOS SDK but I know nothing about Objective C to begin implementing it. This is the correct way of doing it but I could only work on the php side of things which is why I chose the web solution. I'm curious to know how people are posting high scores to a web based table though as effectively they should run into the same problems as mentioned here.

I know you could use the TCP socket commands to send and read data to and from the php script which wouldn't exit your app, but it would mean parsing the permissions html screen when first shown by facebook in code. Not a nice bit of coding to do.
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-22
For my online scores I use netwebget$ and works like a charm, but theres no need to call a web browser there.
With this however we need to pass info via a browser and thats the problem.
How could we implement the Facebook iOS SDK? Because to be honest Spicy, this is the way to do it, just as you mentioned earlier. Anyone knows how to do this for use with Glbasic? Please this is an interesting thing for glbasic. If someone at least knows and maybe tell me what I need to do step by step I would do it. At the moment I havent imported anything so quite null on the matter.

Hope this can be sorted my game is almost finished. Thanks
Title: Re: Facebook connection
Post by: Moebius on 2011-Jul-22
NETWEBEND isn't that bad.  There is multitasking on iOS and on Android, and at least it will work on all platforms.

The platform specific FB APIs (i.e. iOS and Android) won't work with others, and both involve the user using the facebook apps on those devices to sign in.  Perhaps a good approach would be using "Web Views" (if anyone is familiar with that) if you want an easier way to do platform specific FB logins...
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-22
I know netwebend isnt that bad, but can't be used. I post with netwebend to facebook fine as stated in my other post, but then I can't restore control to my app. Ive tried it and theres no way. I end up hitting home button and everything exits. So won't work. And if any player that plays your app and posts to facebook without coming back to game is not going to be very happy. He'll just give up and think, what the *love*. Im not posting again. And what if I want to check if user has posted successfully?

Thanks
Title: Re: Facebook connection
Post by: Moebius on 2011-Jul-22
The idea is that the user has to login ONCE and grant permissions, THEN you can post using the php script  :good:
Title: Re: Facebook connection
Post by: Omadan on 2011-Jul-22
You still need to run the url via the browser mate.
I have done all this. Once permissions are granted you no longer see the Grant permissions etc, the post just goes through to facebook, BUT, it stays there on a blank browser page, with no way to go back to your running game. If you do know how to overcome this please let me know as I have exhausted all options.
Title: Re: Facebook connection
Post by: spicypixel on 2011-Sep-02
I haven't replied here for a while but I was thinking this could be useful to allow the facebook permission and assist in allowing a return to GLB
http://www.glbasic.com/forum/index.php?topic=6708.0 (http://www.glbasic.com/forum/index.php?topic=6708.0)
Title: Re: Facebook connection
Post by: Omadan on 2011-Sep-02
Hey Spicy, been missing you for some time now :P
I gave up on FB thingy, I was losing lots of time on it and losing hours which I could dedicate to my game ofcourse :P

I went the browser way, but is sucks coz app ends and you have no control over it, but sometimes you can't have bread buttered on both sides :P

If you do however manage this I would really like to know how you did it, you were very helpful and clear last time.

Thanks again mate
Title: Re: Facebook connection
Post by: spicypixel on 2011-Sep-03
Yeh I'm just finishing up on my game so it's one of the things I hope to implement. I'm at the stage of the final 10% takes 90% of the time lol. Lots of loose ends to tie up, niceties to add and of course testing. However I do hope to use this WebUI to do the FB posting so I'll keep this updated when I sort it ;)
Title: Re: Facebook connection
Post by: Omadan on 2011-Sep-03
Nice one mate. Im finishing my game too. Hate finishing games, its the worst thing for me, you want it done you thought you just had to do 2-3 more things and hahahaha, 2-3 things quickly turn into 20-30 things. I dont like this, this is better here etc etc. So much one person can do really. :P