Facebook-php-sdk "Session has expired at unix time" with offline_access - php

I want to make wall post to people's friends ( offline )
I took permissions for wall post and offline access.
But most of the users have error when I try to offline wall post
"Fatal error: Uncaught OAuthException: Error validating access token:
Session has expired at unix time 1332190800. The current unix time is
1339803350. thrown in /home/......"
I think access token with offline access never expired ?
If it is expired, How can I take the new access token without touch the people ( offline )
I used this code
$post = array(
'access_token' => "ACCESSTOKEN",
'message' => test",
'link' => 'http://apps.facebook.com/xx',
'description' => 'test'
);
$result = $facebook->api("/FRIENDS_ID/feed/",'post', $post);

The Facebook offline_access permission was deprecated last month. This feature is scheduled to be removed on July 5.
The new workflow requires you to get a long-term access token, which must be renewed every 60 days. There is more information about this here: https://developers.facebook.com/blog/post/2011/05/13/how-to--handle-expired-access-tokens/

Related

Refresh token not changing QuickBooks-V3-PHP-SDK OAuth 2.0

As per sdk documentation.
https://intuit.github.io/QuickBooks-V3-PHP-SDK/authorization.html
"That is what refresh token used for. It is used to request a new access token after access token expired, so you can still access to the QBO company after an hour. Just remember, whenever you make a refreshToken API call, always STORE THE LATEST REFRESH TOKEN value in your session or database"
This is the case: So my access token is expired I need to get a new one I'll run the code below.
$loginHelper = new OAuth2LoginHelper($this->clientID, $this->clientSecret);
$token = $loginHelper->refreshAccessTokenWithRefreshToken($this->refreshToken);
Then the token variable in code above will give me the new access token using this code.
$token->getAccessToken();
It was successful I have the new access token now.
However, seems refresh token is not changing quickbooks said "STORE THE LATEST REFRESH TOKEN" but the $token obj returns are the same.
My refresh token use to request new access token is:
string(50) "Q011532218006WxAY7UnIdulGDJAcgnaoTi7pIkHVaaQld9Me3"
then the return of my request:
QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken Object
(
[accessTokenKey:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => eyJlbmMiOiJBMTI4Q0JDLUhTMjU2IiwiYWxnIjoiZGlyIn0..llxj6kOGCzFiqBShGPUtbg.rNbEdMvhD0iER3z4IwB-azYbbk9givKxtaiDh7Uch8HbmZLkF7-xckpgiMDk8V5lYwYOCfP1EaPFa0iJlpbNPZdT7RBfPIkEqotTVfP0UNPwC13a37PpYHOvg_Sxbp_5GuOvdIa1RVskn4tY_FIOaCVnTaxuDF4MWNKOxsitHEoYxzVVcdqigTWri9yfIA6ZqO2vTAc92iTsa30L9jYcFeEdaNVaPt7zyM-y_FzY2W72uq8fyYjpv-fmvWMwv94pCJ6VoV82rM7OmLh5sOWORKfcZJ0xDLMrKddUPnA0GVl3mb3E6D-JyQ2-N2AYNW553nryI9f9iuoAqv7f6bXhzmaO-ByN0xtDEt9E48CFN-tyA_qShtwmEUajhsq0XFrUZgR0rtEq2kSvj79v3-sX1P7qCjPPrGK2q12VQ24DbVWnDYDZslYh9nyJGtiZgXu-GWFNKhfaXI76U_ASC2yefrcWu6ioIdDrn8j75B9AZgg3GpBqkT7H2qHWGqieeuJmcuFVoYIJUiztZNrDWYvmCJv_w2KlG911GHqLO7K7AznzpiSdIWwGXVBSxYRBbCVQTFJge1SYkXYOWj1OP9aFttpxRNX_ctiAiqV5IA9JBs_T3H4w6mGYRt4NpXZCMn3Yguwc1LekmCn0r83taHjvOT0_l78c1zv2nWC_ADKgdd1lPLMzQHiNWnHCDIBrRYm0.h0WaCjevbfdig0G-zQM9eA
[tokenType:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => bearer
[refresh_token:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => Q011532218006WxAY7UnIdulGDJAcgnaoTi7pIkHVaaQld9Me3
[accessTokenExpiresAt:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => 1523499723
[refreshTokenExpiresAt:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => 1532218006
[accessTokenValidationPeriod:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => 3600
[refreshTokenValidationPeriod:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => 8721883
[clientID:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => Q032lsDMTnotxhcOudLOpyfYuDXfB9xEMWpMRpvFIwW1VV1eyQ
[clientSecret:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] => Ml3Id7ZgeVNzAbxecHwwwzrBmuKeQXw7kljlwT84
[realmID:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] =>
[baseURL:QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken:private] =>
)
As per quickbooks developer, "The previous refresh token expires after 24 hours after you create a new one. So you would get the same refresh token from the time you refresh untill the next 24 hours."
Read more: Click Here

Do I still need to use Facebook user access tokens to post to Facebook pages?

I've created and stored permanent facebook page access tokens. Must I still use a user access token each time I want to post to a fb page feed, or can I use only the page access token?
Here is command I'm using to setup the post now:
$page_post = (new FacebookRequest( **$session**, 'POST', '/'. $page_id .'/feed', array(
'access_token' => $access_token,
'name' => 'Randy Steel',
'link' => 'http://www.LIPSapp.com/',
'caption' => 'Example text',
'message' => 'This is my link!',
) ));
Where $session holds the user access token and $access_token holds the permanent page access token.
If, because I have the perm page access token, I can skip getting/updating the
user access token (60 days at best), how would the above command change?
What would the value of $session need to be, or can that parameter be omitted?
Thanks in advance for any help.
With Facebook's new update like 2 years ago, pages acts like normal profile pages. So you only need access tokens of page not administrator's. Access tokens can be changed overtime or can be expired so it's best you to store administrators access token just in cas the access key losts its validity.
If you always want to post "as Page", you can use the stored Page Token - in fact, you HAVE to use the Page Token to post "as Page". You only need the User Token once, for getting an Extended Page Token.
I would not even use the PHP SDK for that, basic CURL calls are good enough. A small example of using CURL with the Facebook API can be found here: http://www.devils-heaven.com/extended-page-access-tokens-curl/

how to get a fresh access_token in facebook?

I am using the following script to automatically post to my PAGE while i'm offline every specific interval.
$attachment2 = array('message' => 'titlexxxxx',
'access_token' => 'CAAEHzeckZBHABAErwFP08GdHyuJcTY7gV2kVP08GdHyuJcTY7gV2kVP08GdHyuJcTY7gV2kVP08GdHyuJcTY7gV2kVP08GdHyuJcTY7gV2kVP08GdHyuJcTY7gV2kVf6Homc7ful2koSXv72PYQeP08GdHyuJcTY7gV2kVZCKKQZBAU',
'name' => 'titlexxxxxxxxx',
'caption' => 'titlexxxxxxxxx',
'link' => 'linkurllllll',
'description' => 'dexcccc'
);
$result = $facebook->api('/209311095776218/feed/','post',$attachment2); // the number is the page id
I'm getting the access token manually from the facebook graph explorer, I know this is stupid but I can't figure out how to get a fresh realtime access_token.
The access_token is not changing every long time, but it does change and this is annoying.
I need a detailed example to get a fresh token ...
IMPORTANT: the script is running while I'm Offline.
You get a new access token by going through a login flow https://developers.facebook.com/docs/facebook-login/getting-started-web/
For example in PHP it's something simple as allowing the user to click the result of getLoginUrl()
For pages you can go through scenario 5 described at https://developers.facebook.com/roadmap/offline-access-removal/
Extend your short lived access token for a long lived token
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
After this when requesting a page access token (`graph.facebook.com/PAGE_ID?fields=access_token), it will not expire.

Facebook Fatal error Session has expired

<?php
require_once"includes/strings.php";
require_once"php/function.php";
require_once"php/config.php";
require_once"php/app.php";
$currentFBuserid = "0";
///////FACEBOOK GET USER INFORMATION
session_start();
// Enter the app id and secret below
define('YOUR_APP_ID', '$fbAppID');
define('YOUR_APP_SECRET', '$fbAppSecret');
require_once"php/fbsdk/src/facebook.php";
$facebook = new Facebook(array(
'appId' => $fbAppID,
'secret' => $fbAppSecret,
));
$userId = $facebook->getUser();
if($userId){
$userInfo = $facebook->api('/' + $userId);
}
Okay the issue is simple about every two hours I guess the session expires on my site and the error "Fatal error: Uncaught OAuthException: Error validating access token: Session has expired at unix time 1352329200.
The current unix time is 1352330589. thrown in /home/######/public_html/php/fbsdk/src/base_facebook.php on line 1238"
I'm having an impossible time figuring out what the issue is, no matter how long I scrape this site.
BTW:(refreshing the page solves the issue)
I really need help.
Thank you!
Okay the issue is simple about every two hours I guess the session expires on my site and the error "Fatal error: Uncaught OAuthException: Error validating access token: Session has expired at unix time 1352329200.
User access tokens acquired using the client-side flow expire after two hours.
You can either extend them, or use the server-side flow – both will result in a token that’s valid for 60 days.
https://developers.facebook.com/roadmap/offline-access-removal/
(The latest version of the PHP SDK has a method called setExtendedAccessToken for extending a short-lived token.)
BTW:(refreshing the page solves the issue)
If you are using the JS SDK to do the login, then a refresh of course makes it do a new cross-domain request to Facebook, and will get a fresh short-lived access token.

Post to Facebook Page Wall without logged in user

I have a Facebook app that I want to use to post on my customer's Facebook Page Walls on their behalf. I have it setup now so that when they authenticate my app I get the Access token, but when I go to post to the wall I am getting the following error: "OAuthException: Error validating access token: The session is invalid because the user logged out."
How can I post from my script without being logged in?
Here is my code:
include_once('fb-php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
try {
$page_id = '215133535279290'; //page id of the customer's facebook page
$args = array(
'access_token' => 'XXX', //access token received when user authenticated app
'message' => 'A test message'
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
} catch (FacebookApiException $e) {
echo "Error:" . $e;
}
The new facebook API has what is called an extended access token (it replaces offline access: https://developers.facebook.com/roadmap/offline-access-removal/ I think) which I believe lasts upto 60 days (or something like that). To use it simply get the access token when the logs in through your website:
$token = $facebook->getAccessToken();
Save it to Db and use this token later:
$facebook = new Facebook();
$facebook->setAccessToken($accessTokenFromDB);
Then you should be able to continue as though the user is logged in.
As for the user being logged out part, make sure you are using the latest version of the API and not an old version and also make sure the stuff under the "Scenario 2: If you have been previously requesting offline_access - updated 4/30/2012" heading of the page I linked.
You could carry on using offline_access until 3rd of October, but then why if it's being turned off for real in 2 months time and would have to rewrite your code.
Get the publish_stream extended permission. Once you have this permission you can post feed stories using your app access token while the user is offline. There is no need for offline_access permissions or saving and using a user access token in this scenario.

Categories