调用POST时出错https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart:(401)需要登录

时间:2015-03-19 16:51:55

标签: php file-upload google-drive-api google-api-php-client

我想上传一个文件到谷歌驱动器,因为我使用了以下代码和谷歌驱动器sdk但我收到以下错误,错误调用POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart :( 401)需要登录。关于google drive sdk与php集成的知识不多。请帮忙解决。

<?php

    include_once 'google-api-php-client-master/src/Google/Client.php';
    include_once 'google-api-php-client-master/src/Google/Service/Drive.php';
    include_once 'google-api-php-client-master/src/Google/Service/Oauth2.php';

    $client = new Google_Client();

    $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
    $client->setClientId('------');
    $client->setClientSecret('---------------');
    $client->setRedirectUri('https://www.example.com/oauth2callback');


    $service = new Google_Service_Drive($client);

    $data = file_get_contents("a.jpg");

    // create and upload a new Google Drive file, including the data
    try
    {
    //Insert a file
    $file = new Google_Service_Drive_DriveFile($client);

    $file->setTitle(uniqid().'.jpg');
    $file->setMimeType('image/jpeg');

    $createdFile = $service->files->insert($file, array('data' => $data,'mimeType' => 'image/jpeg','uploadType' => 'media',));
    }
    catch (Exception $e)
    {
        print $e->getMessage();
    }

    ?>

1 个答案:

答案 0 :(得分:0)

您必须在Google Developers Console下设置新项目。您可以在此处详细了解该流程以及示例项目:https://developers.google.com/drive/web/examples/php