Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Google OAuth 2.0-用户每次刷新都需要重新同意吗?_Php_Json_Youtube Api_Google Oauth - Fatal编程技术网

Php Google OAuth 2.0-用户每次刷新都需要重新同意吗?

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['

我一直在创建一个小脚本,从用户的YouTube频道获取YouTube每月的浏览量。此脚本工作正常,但每次刷新页面时,都必须重新发送访问权限。我更希望这种情况消失,但我无法在网上找到解决方案

我已经看过刷新令牌,但即使我在刷新时添加它们,用户仍然需要重新请求才能再次获取数据,这可能与创建用户会话或存储cookie有关

这是我目前的工作脚本

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,而不是自己尝试。