Php 无法对令牌进行json解码

Php 无法对令牌进行json解码,php,Php,这是我的密码 <?php session_start(); $url = 'http://localhost:8080/google-drive/index.php?msg=Successfully Uploaded!'; require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_DriveService.ph

这是我的密码

   <?php
session_start(); 
$url = 'http://localhost:8080/google-drive/index.php?msg=Successfully Uploaded!'; 
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();

if(isset($_POST['submit'])){ 
$files = glob('files/*'); // get all file names
foreach($files as $file){ // iterate files
  if(is_file($file))
    unlink($file); // delete file
}

$uploaddir = 'files/';
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['name']);
move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $uploadfile);
}
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
    if ($file != '.' && $file != '..') {
        $files[] = $file;
    }
}
$dir->close();

if (!empty($_POST)) {

    $token = file_get_contents('auth.txt');
    echo $token;
    $token = array(
    'access_token' => 'xxxxxx',
    'token_type' =>'Bearer',
    'expires_in' =>'3600',
    'refresh_token' => 'xxxxx',
    'created' => 'xxxxxxx'
   );

 $tokennew = json_encode($token);
 $client->setClientId('xxxxxxxxxx');
$client->setClientSecret('xxxxxx');
$client->setAccessType('offline');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
    $client->setAccessToken($tokennew);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();
    foreach ($files as $file_name) {
        $file_path = 'files/'.$file_name;
        $mime_type = finfo_file($finfo, $file_path);
        $file->setTitle($file_name);
        $file->setDescription('This is a '.$mime_type.' document');
        $file->setMimeType($mime_type);
        $service->files->insert(
            $file,
            array(
                'data' => file_get_contents($file_path),
                'mimeType' => $mime_type
            )
        );
    }
    finfo_close($finfo);
    header('location:'.$url);
    exit;

}
include 'app.php'; 

?>


当我运行这个文件时,我得到了“未捕获异常”Google_AuthException,消息是“无法json解码令牌”。我想从文本文件中读取访问令牌并传递给setAccessToken()函数。access token打印得很好。我哪里错了?

您能提供auth.txt的内容吗?函数
json\u encode
用于将数组转换为json对象。auth.txt文件的内容应采用正确的格式。如果auth.txt包含json对象,则不需要json_编码,而是使用:

$client->setAccessToken(file_get_contents('auth.txt'));

能否提供auth.txt的内容?函数
json\u encode
用于将数组转换为json对象。auth.txt文件的内容应采用正确的格式。如果auth.txt包含json对象,则不需要json_编码,而是使用:

$client->setAccessToken(file_get_contents('auth.txt'));

以前版本中的方法
setAccessToken
希望访问令牌位于JSON编码的对象中。更新库,或使用JSON编码的对象:

$client->setAccessToken(file_get_contents('auth.txt'));

以前版本中的方法
setAccessToken
希望访问令牌位于JSON编码的对象中。更新库,或使用JSON编码的对象:

$client->setAccessToken(file_get_contents('auth.txt'));

可能重复。可能重复。@Kristi Jorgji查看我编辑的代码。现在它给出了无效的grant Error--“刷新OAuth2令牌时出错,消息:'{“Error”:“invalid_grant”}'@Kristi Jorgji查看我编辑的代码。现在它给出了无效的grant Error--“刷新OAuth2令牌时出错,消息:'{“Error”:“invalid_grant”}”