简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM