上传到youtube(v3),在getAccessToken失败

时间:2014-04-30 19:54:13

标签: php youtube-api

    require_once ('Google/Client.php');
    require_once ('Google/Http/MediaFileUpload.php');
    require_once ('Google/Service/Drive.php');

    $client = new Google_Client();
    $client->setClientId('clitentid');
    $client->setClientSecret('secret');
    $client->setRedirectUri('/');
    $client->addScope("https://www.googleapis.com/auth/drive");
    $service = new Google_Service_Drive($client);
    if ($client->getAccessToken())
    {
        echo __LINE__;die;
        $file = new Google_Service_Drive_DriveFile();
        $file->title = "Big File";
        $chunkSizeBytes = 1 * 1024 * 1024;

        // Call the API with the media upload, defer so it doesn't immediately return.
        $client->setDefer(true);
        $request = $service->files->insert($file);

        // Create a media file upload to represent our upload process.
        $media = new Google_Http_MediaFileUpload(
            $client,
            $request,
            'text/plain',
            null,
            true,
            $chunkSizeBytes
        );
        $media->setFileSize(filesize('myfile'));

        // Upload the various chunks. $status will be false until the process is
        // complete.
        $status = false;
        $handle = fopen('myfile', "rb");
        while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
        }

        // The final value of $status will be the data from the API for the object
        // that has been uploaded.
        $result = false;
        if ($status != false) {
            $result = $status;
        }

        fclose($handle);
    }
    else
    {
        echo 'fail';
    }

客户端ID和秘密来自我可以下载的JSON代码(client_id和client_secret字段)。 所以,它失败了。我哪里可以失败呢?

编辑:$client->getAuth()->getAccessToken()返回'[]'

1 个答案:

答案 0 :(得分:1)

这是php客户端库的错误,只是修复了。

https://github.com/google/google-api-php-client/pull/181

下载主分支。