Reputation: 583
I'm trying to follow Google's Youtube API on resumable upload in PHP/Curl before I go full C/Curl. Here is my code.
<?php
$resource = "www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails";
$query = '{
"snippet": {
"title": "My video title",
"description": "This is a description of my video",
"tags": ["cool", "video", "more keywords"],
"categoryId": 22
},
"status": {
"privacyStatus": "public",
"embeddable": True,
"license": "youtube"
}
}';
$response = NULL;
# Initialize PHP/CURL handle
$ch = curl_init();
$request_headers = array();
$request_headers[] = 'Authorization: Bearer *****';
$request_headers[] = 'Content-Length: 278';
$request_headers[] = 'Content-Type: application/json; charset=UTF-8';
$request_headers[] = 'X-Upload-Content-Length: 3000000';
$request_headers[] = 'X-Upload-Content-Type: video/*';
curl_setopt($ch, CURLOPT_HTTPHEADER , $request_headers );
curl_setopt($ch, CURLOPT_POSTFIELDS , $query );
curl_setopt($ch, CURLOPT_POST , TRUE );
curl_setopt($ch, CURLOPT_HTTPGET , FALSE );
curl_setopt($ch, CURLOPT_HEADER , FALSE ); // Include head as needed
curl_setopt($ch, CURLOPT_NOBODY , FALSE ); // Return body
curl_setopt($ch, CURLOPT_URL , $resource ); // Target site
curl_setopt($ch, CURLOPT_VERBOSE , TRUE ); // Minimize logs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE ); // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION , TRUE ); // Follow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE ); // Return in string
# Create return array
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$error = curl_error($ch);
# Close PHP/CURL handle
curl_close($ch);
return $response;
?>
Whenever I run the script I get a 403 Forbidden error. I think the issue comes from the authorization token. I think I'm not using what I am supposed to. Google says
"The request sets the following HTTP request headers:
- Authorization – The authorization token for the request."
I am not sure what it is exactly, so I used an access token generated with Youtube PHP Client API but still nothing.
Upvotes: 1
Views: 1664
Reputation: 1600
check this code. Maybe this can help you.
In this, you can get the location parameter in the response header. Please see below code and response header that I got. Also, use clean and clear JSON
data in $requestBody
<?php
function startResumableSession() {
$requestBody = '{"snippet":{"title":"test video","description":"testing api","tags":["any","thing"],"categoryId":25},"status":{"privacyStatus":"public","embeddable":"true","license":"youtube"}}';
$headers = array
(
"POST /upload/youtube/v3/videos?uploadType=resumable&part=snippet,status HTTP/1.1",
"Host: www.googleapis.com",
"Content-length: 0",
"Content-type: application/json",
"Authorization: Bearer xxxxxxxxxxxxxACCESS_TOKENxxxxxxxxxxxxxxxxxx",
);
/* Parameter values in the request URL must be URL-encoded. */
$url = "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS,urlencode($requestBody));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_PROXY, "192.168.10.5:8080");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$result = curl_exec($ch);
$info = curl_getinfo($ch);
$json = json_decode($result);
/* debug info */
return array('response'=>(array)$result,
'info' => $info,
'headers'=>$headers
);
}
This is the header of the response. In this, you can find the location with upload id. For next step please visit this link Save the resumable session URI
HTTP/1.1 200 Connection established
HTTP/1.1 200 OK
X-GUploader-UploadID: AEnB2UqLAR0Gf5xGi9pDGEYgMkrajFAuaC33IpiY-2WM2hspQe30DxUz2qzELRenLWOyrK8x9S3He51SXGmHU6olQzUGqGxbcw
Location: https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
ETag: "m2yskBQFythfE4irbTIeOgYYfBU/V3uOxfY0pSsHgtj2XMjOGKOAp3o"
Vary: Origin
Vary: X-Origin
X-Goog-Correlation-Id: Z14VW6zFX9E
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Mon, 01 Jan 1990 00:00:00 GMT
Date: Fri, 16 Jun 2017 08:36:22 GMT
Content-Length: 0
Server: UploadServer
Content-Type: text/html; charset=UTF-8
Alt-Svc: quic=":443"; ma=2592000; v="38,37,36,35"
Upvotes: 2
Reputation: 169
https://www.googleapis.com/youtube/... is needed now (https:// not just www)
also from youtube api site
In the list of APIs, make sure the status is ON for the YouTube Data API v3.
If your application will use any API methods that require user authorization, read the authentication guide to learn how to implement OAuth 2.0 authorization.
Upvotes: 1