Google OAuth:没有refresh_token

时间:2014-05-22 09:18:11

标签: google-oauth google-api-php-client

我正在尝试使用google-api-php-client将视频上传到Youtube。

由于我只需要访问一个Youtube频道,而不是许多用户频道,我必须一次授权我的应用并存储refresh_token以实现对API的过期访问。

问题是,我refresh_token没有$client->getAccessToken()来电。{ 我只获得以下值:token_type,expires_in,id_token,created

我为网络应用程序创建了一个"客户端ID"使用Google控制台并使用以下代码:

<?php
$OAUTH2_CLIENT_ID = '<my client id>';
$OAUTH2_CLIENT_SECRET = '<my client secret>';

$client = new Google_Client();
$client->setApplicationName('<my app name>');
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('profile email https://www.googleapis.com/auth/youtube');
$client->setState('offline');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
    FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);

if(isset($_GET['code'])) {
    $client->authenticate($_GET['code']);
    $accessToken = $client->getAccessToken();
    var_dump($accessToken, json_decode($accessToken));
}
else {
    $aurl = $client->createAuthUrl();
    $aurl = str_replace('approval_prompt=auto', 'approval_prompt=force', $aurl);
    echo '<a href="'.$aurl.'">'.$aurl.'</a>';
}

如您所见,我已使用$client->setState('offline')并在authURL中插入apprival_prompt=force

我还尝试多次撤销Google帐户管理员中的访问权限,并创建了一个新的API项目。

提前感谢您告诉我我做错了什么:)

1 个答案:

答案 0 :(得分:0)

我认为问题是$accessToken = $client->getAccessToken();是一个帖子。

client->authenticate($_GET['code']);  
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

需要将其发布到Google。当它回来时你应该有$_SESSION['token']

完全未经测试的示例:

<?php         
require_once 'Google/Client.php';     
require_once 'Google/Service/YouTube.php';       
session_start();      
$client = new Google_Client();
    $client->setApplicationName("Client_Library_Examples");
    $client->setDeveloperKey("{devkey}");  
    $client->setClientId('{clientid}.apps.googleusercontent.com');
    $client->setClientSecret('{clientsecret}');
    $client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

    //For loging out.
    if ($_GET['logout'] == "1") {
    unset($_SESSION['token']);
       }   

    // Step 2: The user accepted your access now you need to exchange it.
    if (isset($_GET['code'])) {

        $client->authenticate($_GET['code']);  
        $_SESSION['token'] = $client->getAccessToken();
        $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
        header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
    }

    // Step 1:  The user has not authenticated we give them a link to login    
    if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
        $authUrl = $client->createAuthUrl();
        print "<a class='login' href='$authUrl'>Connect Me!</a>";
        }        

    // Step 3: We have access we can now create our service
    if (isset($_SESSION['token'])) {
        print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
        $client->setAccessToken($_SESSION['token']);
        $youtube = new Google_Service_YouTube($client);
    }


 print "Access from google: " . $_SESSION['token']; 
?>

这样的事可能。对不起,我不能从这里测试它来自其他一些代码的编辑。如果它不起作用,请告诉我,我会尽力在今晚进行测试。