get a new short-lived user access_token

半城伤御伤魂 提交于 2019-12-13 01:04:57

问题


I need to renew a long-lived access token. I read Renew long lived access token server side topic and wrote a code as follows:

<?php

$code = $_REQUEST["code"];

if(empty($code)) {

  $dialog_url = "https://www.facebook.com/dialog/oauth?"
    . "client_id=$app_id"
    . "&redirect_uri=$my_url"
    . "&scope=..."
    ;

  echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
else
{
  $response = file_get_contents("https://graph.facebook.com/oauth/access_token?"
    . "client_id=$app_id"
    . "&redirect_uri=$my_url"
    . "&client_secret=$app_secret"
    . "&code=$code"
    );

  $params = null;
  parse_str($response, $params);
  $access_token=$params['access_token'];

  $response = file_get_contents("https://graph.facebook.com/oauth/access_token?"
    . "client_id=$app_id"
    . "&client_secret=$app_secret"
    . "&redirect_uri=$my_url"
    . "&grant_type=fb_exchange_token"
    . "&fb_exchange_token=$access_token"
    );
}

?>

On the first invocation it acquires 60-days access token all right. I expect that on the next invocations it would acquire another (may be with the same name) 60-days tokens, and I would see in the Debugger https://developers.facebook.com/tools/debug that issue time and expiration time changes, but the times do not change. What's wrong with my scenario?


回答1:


Have you compared the tokens to each other? Facebook will send you back the existing access token when the same call is made in less than 24 hours. Also, tokens are set to not expire if you have also requested page tokens for the user. See my answer here: Facebook Page Access Tokens - Do these expire? for more info on this subject.

One way you can be sure to get a new token each time is if you revoke access by making an http DELETE call to /PROFILE_ID/permissions and then requesting a new token. The only bad thing about this is it will require you to put the user through the oAuth dialog again.



来源:https://stackoverflow.com/questions/13841174/get-a-new-short-lived-user-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!