Shade- / MyFacebook-Connect

A plugin to integrate Facebook with MyBB, letting users login and register through Facebook.
27 stars 24 forks source link

Provide Facebook access token #37

Closed Radiergummi closed 9 years ago

Radiergummi commented 9 years ago

Hey there, currently trying to provide custom open graph stories for my users (like, {user} posted a new thread on {site}) and was wondering if I can get a valid access token from MyFBConnect somehow?

Shade- commented 9 years ago

Yes you can. Assuming you have configured your Facebook application correctly and you have added the API tokens to the ACP settings panel, require the Facebook class (located @ /inc/plugins/MyFacebookConnect/class_facebook.php) and initialize the object ($FacebookConnect = new MyFacebook();).

Set a fallback URL ($FacebookConnect->set_fallback(YOUR_URL_HERE)). This will be the URL where your users will be redirected once they log in. You will need to replicate the same code structure as the one you see in the file /myfbconnect.php, where the action is do_login. That's the default fallback URL.

Authenticate users. $FacebookConnect->authenticate();. They will be redirected automatically to the fallback URL you've set before.

In the fallback code, replace the process(); function (which handles all the MyBB login part) with $token = $FacebookConnect->facebook->getAccessToken();. This is your short lived access token, and you can do what you want with it using Facebook's methods. You can even use $FacebookConnect->facebook class, which is already loaded, to perform Graph API queries.

Depending on how your application will communicate with your users, you might need to obtain and set a long lived access token. This is easily done firing a $FacebookConnect->facebook->setExtendedAccessToken(); before getting the access token. This ensures the access token generated will persist for at least 70 days.