我该如何使用curl刷新令牌将文件自动发送到Google驱动器,而无需用户动手?

时间:2019-04-02 07:53:03

标签: php curl google-api google-drive-api google-oauth

我需要将文件上传到google驱动器,我可以使用此脚本执行此操作,但是我的到期时间是3600秒,之后google再次要求授权。 3600秒后出现此错误:

  

Array([error] => Array([errors] => Array([0] => Array([domain] =>全局[reason] => authError [message] =>无效的凭证[locationType] =>标头[位置] =>授权))[代码] => 401 [消息] =>无效的凭证))

访问令牌正常工作,但在一段时间后刷新令牌无效。我怎样才能解决这个问题?预先感谢。

<?php 

error_reporting(E_ALL);
ini_set("display_errors", 1);

$GAPIS = 'https://www.googleapis.com/'; 
$GAPIS_AUTH = $GAPIS . 'auth/'; 
$GOAUTH = 'https://accounts.google.com/o/oauth2/'; 
$CLIENT_ID = 'X'; 
$CLIENT_SECRET = 'X'; 
$REDIRECT_URI = 'http' . ($_SERVER['SERVER_PORT'] == 80 ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME']; 
$SCOPES = array($GAPIS_AUTH . 'drive', $GAPIS_AUTH . 'drive.file', $GAPIS_AUTH . 'userinfo.email', $GAPIS_AUTH . 'userinfo.profile'); 
$STORE_PATH = 'credentials.json'; 

function uploadFile($credentials, $filename, $targetPath) {
    global $GAPIS; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $GAPIS . 'upload/drive/v2/files?uploadType=media'); 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type : application/pdf', 'Content-Length:' . filesize($filename), 'Authorization: Bearer ' . getAccessToken($credentials)) );
    $postResult = curl_exec($ch);
    curl_close($ch); 
    return json_decode($postResult, true);

} function getStoredCredentials($path) {
    $credentials = json_decode(file_get_contents($path), true); 
    if (isset($credentials['refresh_token'])) 
    return $credentials; $expire_date = new DateTime(); 
    $expire_date->setTimestamp($credentials['created']); 
    $expire_date->add(new DateInterval('PT' . $credentials['expires_in'] . 'S')); 
    $current_time = new DateTime(); 
    if ($current_time->getTimestamp() >= $expire_date->getTimestamp()) {
        $credentials = null; unlink($path); 
    } return $credentials; 

} function storeCredentials($path, $credentials) {
    $credentials['created'] = (new DateTime())->getTimestamp(); 
    file_put_contents($path, json_encode($credentials)); return $credentials; 
} function requestAuthCode() {
    global $GOAUTH, $CLIENT_ID, $REDIRECT_URI, $SCOPES; 
    $url = sprintf($GOAUTH . 'auth?scope=%s&redirect_uri=%s&response_type=code&client_id=%s&approval_prompt=force&access_type=offline', urlencode(implode(' ', $SCOPES)), urlencode($REDIRECT_URI), urlencode($CLIENT_ID) ); 
    header('Location:' . $url); 
} function requestAccessToken($access_code) {
    global $GOAUTH, $CLIENT_ID, $CLIENT_SECRET, $REDIRECT_URI; $url = $GOAUTH . 'token'; 
    $post_fields = 'code=' . $access_code . '&client_id=' . urlencode($CLIENT_ID) . '&client_secret=' . urlencode($CLIENT_SECRET) . '&redirect_uri=' . urlencode($REDIRECT_URI) . '&grant_type=authorization_code'; 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    $result = curl_exec($ch); 
    curl_close($ch); 
    return json_decode($result, true); 
} function getAccessToken($credentials) {
    $expire_date = new DateTime(); 
    $expire_date->setTimestamp($credentials['created']); 
    $expire_date->add(new DateInterval('PT' . $credentials['expires_in'] . 'S')); 
    $current_time = new DateTime(); 
    if ($current_time->getTimestamp() >= $expire_date->getTimestamp()) return $credentials['refresh_token']; 
    else return $credentials['access_token']; 
} function authenticate() {
    global $STORE_PATH; 
    if (file_exists($STORE_PATH)) $credentials = getStoredCredentials($STORE_PATH); 
    else $credentials = null; 
    if (!(isset($_GET['code']) || isset($credentials))) requestAuthCode(); 
    if (!isset($credentials)) $credentials = requestAccessToken($_GET['code']); 
    if (isset($credentials) && isset($credentials['access_token']) && !file_exists($STORE_PATH)) $credentials = storeCredentials($STORE_PATH, $credentials); return $credentials; 
} 

$credentials = authenticate(); 
$result = uploadFile($credentials, 'vcard.pdf', '');
if (!isset($result['id'])) throw new Exception(print_r($result)); 
else echo 'OK (file Id: ' . $result['id'] . ')';

?>

0 个答案:

没有答案