2016-04-07 15 views
1

は私のコードは、は、JSONのデコード、私は私が 『というメッセージと『「キャッチされない例外』Google_AuthExceptionを持って、このファイルを実行したときにここでトークン

<?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'; 

?> 

では、JSONは』トークンをデコードすることができませんでしたができませんでした。テキストファイルからアクセストークンを読み込み、setAccessToken()関数に渡したい。アクセストークンがうまく印刷されます。どこが間違っていますか?

+0

http://stackoverflow.com/questions/23886132/uncaught-exception-google-auth-exception-with-message-could-not-json-の可能性の重複デコード-t。 – IAmInPLS

答えて

0

あなたはあなたのauth.txtの内容を提供できますか?関数json_encodeは、配列をjsonオブジェクトに変換するために使用されます。 auth.txtファイルの内容は適切な形式にする必要があります。 auth.txtはJSONオブジェクトが含まれている場合、あなたはjson_encodeする必要はありません、代わりに使用します。

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

@Kristi Jorgjiは私の編集したコードを見てください。無効な権限を与えましたerrror - 'OAuth2トークンをリフレッシュする際にエラーが発生しました:' {"error": "invalid_grant"} '' – djpro

0

を以前のバージョンでsetAccessTokenは、アクセストークンがJSONエンコードされたオブジェクトであることを期待している方法。ライブラリを更新、または使用JSONエンコードされたオブジェクト:

$client->setAccessToken(file_get_contents('auth.txt')); 
関連する問題