Php Google OAuth 2.0-用户每次刷新都需要重新同意吗?
我一直在创建一个小脚本,从用户的YouTube频道获取YouTube每月的浏览量。此脚本工作正常,但每次刷新页面时,都必须重新发送访问权限。我更希望这种情况消失,但我无法在网上找到解决方案 我已经看过刷新令牌,但即使我在刷新时添加它们,用户仍然需要重新请求才能再次获取数据,这可能与创建用户会话或存储cookie有关 这是我目前的工作脚本Php Google OAuth 2.0-用户每次刷新都需要重新同意吗?,php,json,youtube-api,google-oauth,Php,Json,Youtube Api,Google Oauth,我一直在创建一个小脚本,从用户的YouTube频道获取YouTube每月的浏览量。此脚本工作正常,但每次刷新页面时,都必须重新发送访问权限。我更希望这种情况消失,但我无法在网上找到解决方案 我已经看过刷新令牌,但即使我在刷新时添加它们,用户仍然需要重新请求才能再次获取数据,这可能与创建用户会话或存储cookie有关 这是我目前的工作脚本 if(isset($_GET['code'])) { // try to get an access token $code = $_GET['
if(isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
// this will be our POST data to send back to the OAuth server in exchange
// for an access token
$params = array(
"code" => $code,
"client_id" => $oauth2_client_id,
"client_secret" => $oauth2_secret,
"redirect_uri" => $oauth2_redirect,
"grant_type" => "authorization_code"
);
// build a new HTTP POST request
$request = new HttpPost($url);
$request->setPostData($params);
$request->send();
// decode the incoming string as JSON
$responseObj = json_decode($request->getHttpResponse());
$currentDate = date("Y-m-d");
$monthBefore = date('Y-m-d', strtotime(date('Y-m')." -1 month"));
$views = file_get_contents('https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DMINE&start-date='.$monthBefore.'&end-date='.$currentDate.'&metrics=views&access_token='.$responseObj->access_token);
$json = json_decode($views);
}
$monthlyViews = $json->rows[0][0];
var_dump($monthlyViews);
您不需要每次都重新生成令牌,例如,我建议您使用GoogleOAuthPHPSDK,而不是自己尝试。