Получить токен доступа к отчетам YouTube

Я пытаюсь следовать руководству Использование OAuth 2.0 для межсерверных приложений, чтобы получить токен доступа, чтобы я мог использовать API отчетов YouTube.

Но что бы я ни делал, у меня всегда такой результат:

Warning: file_get_contents(https://www.googleapis.com/oauth2/v4/token): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request 

Вот мой код

$contents = file_get_contents('BCGA-557d334b8526.json');
$account = json_decode($contents, TRUE);
$privateKeyString = $account['private_key'];
$time = time();
$expiration = $time + 3600;

$header = '{"alg":"RS256","typ":"JWT"}';
$claim_set = '{"iss":"[email protected]","scope":"https://www.googleapis.com/auth/yt-analytics.readonly","aud":"https://www.googleapis.com/oauth2/v4/token","exp":"' . $expiration . '","iat":"' . $time . '"}';

$unsignedToken = base64_encode($header) . '.' . base64_encode($claim_set);

$signature = hash_hmac('sha256', $unsignedToken, $privateKeyString, true);

$jwt = base64_encode($header) . '.' . base64_encode($claim_set) . '.' . base64_encode($signature);



$url = "https://www.googleapis.com/oauth2/v4/token";
$data = array('grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', 'assertion' => $jwt);

// use key 'http' even if you send the request to https://...
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context = stream_context_create($options);
$result  = file_get_contents($url, false, $context);
if ($result === FALSE) { /* Handle error */ }

var_dump($result);

Какие-либо предложения?


person allanth    schedule 30.06.2017    source источник


Ответы (1)


Ты забыл

'grant_type' => 'refresh_token'
person jajaja    schedule 28.10.2019