Google Drive API会将所有文件上传到一个帐户

时间:2016-02-29 10:19:06

标签: php google-drive-api

GoogleDrive API PHP上传文件时出现问题。我有两个帐户A和B,一个授权为A用户上传文件,一切正常,比我授权为B用户并尝试上传文件并上传到用户的驱动器!我检查了访问令牌他们是不同的,我授权为B用户。会话很清楚,从C帐户的另一台计算机上尝试,结果相同,它上传到A帐户驱动器,几小时后我可以授权为D用户,所有下一次从其他帐户上传将是他的谷歌驱动器。

我授权为

public static function auth($state, $config) {
    $client = self::getGoogleClient($config);
    $client->setState($state);
    $auth_url = $client->createAuthUrl();
    return filter_var($auth_url, FILTER_SANITIZE_URL);
}

$ config是针数据数组,如client_id,client_secret,redirect_url等。

public static function uploadFile($access_token, $uploadFile, $fileName, $config) {
    if (!isset($access_token)) {
        return;
    }

    $userId = \HttpReceiver\HttpReceiver::get('userId', 'string');
    $client = self::getGoogleClient($config);
    try {
        $access_token = (array)$access_token;
        $client->setAccessToken($access_token);
    }catch (\InvalidArgumentException $e){
        return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth($userId, $config));
    }

    $service = new \Google_Service_Drive($client);

    //Insert a file
    $file = new \Google_Service_Drive_DriveFile();
    new \Google_Service_Drive_User();

    if (!isset($fileName) || strlen($fileName) == 0 || $fileName == '0') {
        $tmp = explode('/', $uploadFile);
        $fileName = $tmp[sizeof($tmp) - 1];
    }else{
        $fileName .= '.'.self::getExtension($uploadFile);
    }

    $file->setTitle($fileName);
    $file->setDescription('A test document');
    $file->setMimeType(self::getMime(self::getExtension($uploadFile)));
    $data = file_get_contents($uploadFile);
    $folderInfo = self::getFolder($access_token, $config);
    $id = 0;
    if($folderInfo['status'] === 'ok'){
        $id = $folderInfo['id'];
    }else{
        return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth($userId, $config));
    }
    $parent = new \Google_Service_Drive_ParentReference();
    $parent->setId($id);
    $file->setParents(array($parent));
    try {
        $createdFile = $service->files->insert($file, array(
            'data' => $data,
            'mimeType' => self::getMime(self::getExtension($uploadFile)),
            'uploadType' => 'media'
        ));

    }catch(\Exception $e){
        return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth($userId, $config));
    }

    if(isset($createdFile) && isset($createdFile['id']) && strlen($createdFile['id']) > 0){
        return array('status' => 'ok');
    }else{
        return array('status' => 'error', 'msg' => 'refreshToken', 'url' => self::auth($userId, $config));
    }
}

$ createdFile有关于所有者的信息,它认为该所有者是A用户,但是我使用了B用户的access_token

获取google客户端功能

private static function getGoogleClient($config) {
    $client = new \Google_Client();

    $config = self::getGoogleConfig($config);

    $client->setAuthConfig($config);

    //$client->setAuthConfigFile($config);

    $userId = \HttpReceiver\HttpReceiver::get('userId', 'int');

    if(!isset($userId)){
        $userId = \HttpReceiver\HttpReceiver::get('state', 'int');
    }

    $client->setRedirectUri($config['GOOGLEDRIVE_REDIRECT2']);
    $client->addScope(\Google_Service_Drive::DRIVE);
    return $client;
}

0 个答案:

没有答案