Generate “never-expire” access token for Facebook Page

后端 未结 12 1913
猫巷女王i
猫巷女王i 2020-11-28 19:49

I have managed to post to Facebook Page via API (C#), but when administrator of the page logs out, the following error occurs:

\"(OAuthException - #190) Error valida

12条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 20:12

    The method below worked for me, if you are using 4.x Facebook SDK:

    1. Create the Temporary User Access Token for the first time using the method mentioned here.
    2. Now! It's time to convert this token to Long Term Token using PHP SDK 4.x. Use the following code as it worked for me:

    //Class for Generating the Long Lived Token

    namespace App\Lib;
    
    use Facebook\FacebookApp;
    use Facebook\FacebookClient;
    use Facebook\Authentication\OAuth2Client;
    
    class FacebookLongLivedTokenGenerator
    {
        public $longLivedTokenGenerated = false;
    
        public function generateFacebookLongLivedToken($appId, $appSecret, $oldToken)
        {
            //request new access token
            $oauth2Fb = new OAuth2Client(new FacebookApp($appId, $appSecret), new FacebookClient());
            $longLivedToken = $oauth2Fb->getLongLivedAccessToken($oldToken);
            if ($longLivedToken) {
                $this->longLivedTokenGenerated = true;
                $this->userAccessToken = $longLivedToken;
            }
            return trim($this->userAccessToken);
        }
    }
    

    You can consume the above class this way:

    $longToken = new FacebookLongLivedTokenGenerator();
    echo $longToken->generateFacebookLongLivedToken($appId, $appSecret, $oldUserAccessToken);
    

提交回复
热议问题