PHP - 404 Video Not Found (Videos: PUT) YouTube API - php

I'm creating a small web app allowing people to update their video outside of YouTube. I'm currently testing for myself and i'm running into the
{ "error": { "errors": [ { "domain": "youtube.video", "reason": "videoNotFound", "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct.", "locationType": "other", "location": "body.id" } ], "code": 404, "message": "The video that you are trying to update cannot be found. Check the value of the \u003ccode\u003eid\u003c/code\u003e field in the request body to ensure that it is correct." } }
Things that I know for certain:
I have the correct authorization code
I have the correct channel this authorization code refers to.
I have the correct video id.
I'm sending a PUT request using cURL in PHP as follows:
$curl = curl_init($url . "https://www.googleapis.com/youtube/v3/videos?part=snippet&access_token=".$token)
$data = array(
'kind' => 'youtube#video',
'id' => 'theCorrectIdIsHere',
'snippet' => array(
"title" => "Test title done through php",
"categoryId" => "1"
),
);`
So how come when I execute this using:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:
application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));`
A similar question has been asked: Update title and description using YouTube v3 API?, however, the answer involved him downloading and replacing the video which costs extra quota and simply shouldn't have to be done
Note Everything works fine on the API test done here: https://developers.google.com/youtube/v3/docs/videos/update

Use json_encode to set the request data & using Authorization header to set access token :
<?php
$curl = curl_init();
$access_token = "YOUR_ACCESS_TOKEN";
$data = array(
'kind' => 'youtube#video',
'id' => 'YOUR_VIDEO_ID',
'snippet' => array(
"title" => "Test title done through php",
"categoryId" => "1"
)
);
curl_setopt($curl,CURLOPT_URL, "https://www.googleapis.com/youtube/v3/videos?part=snippet");
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Authorization: Bearer ' . $access_token));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_VERBOSE, true);
$result = curl_exec($curl);
curl_close($curl);
var_dump($result);
?>

Related

Microsoft Azure Auth token

I ma having an issue with getting my access token. I know I have allowed access in the settings in my app. I can read my email with there text page:
I can use the token they provide and retrieve it. But I need to be able to get my own token of course. When I have code below it opens the login page and says "There was an issue looking up your account. Tap Next to try again."
Am I using the right API?
$curl = curl_init();
$auth_data = array(
"Content-Type: application/x-www-form-urlencoded",
'client_id' => 'my-client-id',
'response_type' => 'code',
'redirect_uri' => 'https://myurl.com/authorize.php',
'response_mode' => 'query',
'scope' => 'https://graph.microsoft.com/.default',
'state' => '12345'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, "https://login.microsoftonline.com/{my_tenant_id}/oauth2/v2.0/authorize");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
//echo $result;
var_dump($result);
//print_r($result);

cURL PUT Request Not Working with PHP

I am working with the Challonge.com API found here: https://api.challonge.com/v1
I am trying to get the match UPDATE feature to work - https://api.challonge.com/v1/documents/matches/update
I have had success updating my tournament with the same code, but for some reason the following code is not updating the variables. Instead the response I get is the same as before the script is ran. Any Ideas?
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
);
$url = "https://api.challonge.com/v1/tournaments/efps_59/matches/78842711.json";
$data_json = json_encode($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
Your documentation states that the parameters for winner_id and scores_csvhave to be an array of match:
// Update Match on Challonge
$params = array(
"api_key" => "my api key goes here",
"match" => array(
"winner_id" => "50287554",
"scores_csv" => "2-5,1-3"
)
);

Google Cloud Speech API using php

I am trying to call Google Cloud Speech API by using PHP and got a problem.
$stturl = "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=xxxxxxxxxxxx";
$upload = file_get_contents("1.wav");
$upload = base64_encode($upload);
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sampleRate" => 16000,
"languageCode" => "en-US"
),
"audio" => array(
"Content" => $upload,
)
);
$jsonData = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
$result = curl_exec($ch);
The result says that it is INVALID JSON PAYLOAD.
{ "error": { "code": 400, "message": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field.", "status":
"INVALID_ARGUMENT", "details": [ { "#type":
"type.googleapis.com/google.rpc.BadRequest", "fieldViolations": [ {
"field": "audio", "description": "Invalid JSON payload received.
Unknown name \"content\" at 'audio': Cannot find field." } ] } ] } } "
I think this is because $upload isn't configured correctly.
According Google Cloud Speech API, it should be "A base64-encoded string".
https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionAudio
That's why I used base64_encode function, but it seems JSON doesn't process this value correctly.
Any thoughts?
You need to construct the properly formatted input as an array and then json encode it. For example, to send a file, base64encode it as "content" and submit to the API as shown:
$data = array(
"config" => array(
"encoding" => "LINEAR16",
"sample_rate" => $bitRate,
"language_code" => "en-IN"
),
"audio" => array(
"content" => base64_encode($filedata)
)
);
$data_string = json_encode($data);
$ch = curl_init($googlespeechURL);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
please make 'content' instead of 'Content'
small letter 'c'
its work for me.

auto share links using php curl

I want to auto share links in my facebook wall using this script :
$attachment = array(
'email' => 'mail',
'password' => 'password',
'access_token' => 'my token',
'message' => "my message",
'name' => 'name',
'link' => 'my_url',
'description' => "my description"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/links');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,5);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101');
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$json = json_decode($result);
$post_id = $json->{'id'};
curl_close($ch);
at the began it worked for me but after trying it for 7 times or more i got this error :
{ "error": { "message": "(#100) The parameter url is required", "type": "OAuthException", "code": 100 } }
how can I fix it
links
Create
You can post a link on the user's behalf by issuing an HTTP POST request to PROFILE_ID/feed with the publish_stream permissions.
The other fields are taken from the metadata of the page URL given in the 'link' param.
If the create is successful, you get the following return.
Refer to: https://developers.facebook.com/docs/reference/api/user/#links
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/me/feed');

Post image on user wall with app message followed by app URL

In my application, I am posting images on walls, and it works fine, but now my requirement is to print my app's URL with a message and have it so that when a user clicks on the link, he is redirected to my app.
You can use curl method, Am using this and it works fine
$attachment = array(
'access_token' => "accesstoken",
'caption' => "Hey -- this is caption",
'description' => "Your Desc",
'link' => "http://www.example.com/",
'picture'=> "path/image.png"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://graph.facebook.com/XXXXXX/feed');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);

Categories