Deleting a video using YouTube API v3 PHP - php

I have the following code to delete a YouTube video using cURL. I'm getting a 401 response/authorization error. I've put in the API key in the url and do I put the access token in the bearer value in CURLOPT_HTTPHEADER area?
$url = "https://www.googleapis.com/youtube/v3/videos?id=VIDEO_ID_HERE&key=KEY_HERE";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/json','Authorization : Bearer '.$_SESSION['access_token']));
$result = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Are there any other mistakes I'm making?

Okay I had another question on the same topic (deleting videos) so I'll post that answer here now that I've figured it out in case someone finds this via Google. I never figured out the cURL method but the method using the google PHP library for v3 of their API is as follows:
$youtube = new Google_Service_YouTube($client);
...
//do your authoraisation stuff + getting access token etc
...
$youtube->videos->delete('<Your Video ID>');
Hope that helps!

Related

YouTube v3 API call for search.list: proper curl setting

I am sending a call to the YouTube API using search.list (retrieve the first search result for a keyword) but I am getting nothing in return.
Here is my call:
$api_url = 'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&type=video&key=MYKEY&&format=json&q=SEARCHTERMS;
Note: MYKEY is my API key I got from Google (currently active) and SEARCHTERMS is any word to search.
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
$value = json_decode(json_encode($data), true);
echo $value;
The URL works correctly (tested on browser) and I'm not getting any kind of error (console), but still this curl request isn't echoing any data from YouTube. Isn't it properly set?
P.s. My quota isn't exceeded.
SOLVED!
Watch out: youtube API doesn't read spaces in url (eg. in search terms), so you gotta streplace all of them with "+"

OAuth1.0 with php and CURL not working

I am trying to hit api which is having OAuth1.0.
Problem is when i am trying with Postman i am getting response but with PHP curl its giving error.
In postman i am passing parameters as below
url='xxxx', Authentication stuff is type=OAuth1.0,Consumer Key,Consumer Secret, Token, Token Secret, Timestamp, Nonce, Version=1.0, Realm=Optional
Below is my code which i have tried
$url="xxx";
$header[] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode('Authorization:OAuth oauth_consumer_key="xxx",oauth_token="xxx",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1475651061",oauth_nonce="LyA5sR",oauth_version="1.0",oauth_signature="xxx"'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
if(curl_error($ch))
{
echo 'error:' . curl_error($ch);
}
echo "<pre>";print_r($result);exit;
I have added CURLOPT_SSL_VERIFYPEER, because there was certification problem. Now i am getting 'Access denied' from api curl error 403 because there is something wrong in the way i am passing for Oauth 1.0 by curl.
This is the first time i am trying outh1.0 with curl. Please help me whats going on in my code. I have tried using this link
The issue is with oauth signature which is not getting formed properly. You can refer https://oauth.net/core/1.0/ for more clarity

how to do pivotaltracker oauth authentication using curl

I am new to curl. I'm trying basic authentication using curl with pivotal tracker
error:{"error":"Needs authentication credentials.","possible_fix":"Try basic auth, or include header X-TrackerToken.","code":"unauthenticated","kind":"error".
here is simple code i used to authenticate:
$ch = curl_init();
$data = array('$token'=>'858e234da*****');
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://www.pivotaltracker.com/services/v5/me");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// grab URL and pass it to the browser
$response =curl_exec($ch);
var_dump($response);
// close cURL resource, and free up system resources
curl_close($ch);
how to authenticate my application with token as pivotal tracker support simple ouath authentication.
i would like to explain steps of authentication
a.)user enter user name and password.
b.)then enter token of your profile.
c.) now page is redirected your dashboard.
Try basic auth, or include header X-TrackerToken.
Which seems to me is asking you to submit the token as a X-TrackerToken header.
You can try something like this, though I can't find the pivotaltracker documentation.
$ch = curl_init('https://www.pivotaltracker.com/services/v5/me');
// or just remove the customrequest too
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'X-TrackerToken: 858e234da*****'
);
$result = curl_exec($ch);
curl_close($ch);
update
Found the documentation.
It shows something like:
export TOKEN='your Pivotal Tracker API token'
curl -X GET -H "X-TrackerToken: $TOKEN" "https://www.pivotaltracker.com/services/edge/stories/558"

Eventbrite API simple access token request doesn't work

My code, which actually perfectly works with Linkedin and Meetup APIs, doesn't work with Eventbrite API and I definitely don't understand why :
$params = array(
'grant_type' => 'authorization_code',
'client_id' => EVENTBRITE_CONSUMER_KEY,
'client_secret' => EVENTBRITE_CONSUMER_SECRET,
);
// Eventbrite API access token request
$url = 'https://www.eventbrite.com/oauth/token?' . http_build_query($params);
// Tell streams to make a POST request
$context = stream_context_create(array('http' => array('method' => 'POST')));
// Retrieve access token information
$response = file_get_contents($url, false, $context);
I precise that the API login part to get the authorization code seems to work perfectly well.
Here is the PHP error :
Warning: file_get_contents(https://www.eventbrite.com/oauth/token?grant_type=authorization_code&client_id=ZZ6PPQOMTKSXIHEKLR&client_secret=QQDDIS4RBZXI6ONO7QEYEUZ4JB2ABQQG6K3H7CBD6M5QWK5GSK&code=O63YZASRAYMOUHRMH5AH): failed to open stream: HTTP request failed! HTTP/1.1 400 BAD REQUEST in /var/www/include/actions.php on line 102
Thanks by advance if anybody has a clue :)
UPDATE :
I finally found where is the problem (even if I don't understand why) :
file_get_contents doesn't seem to be a good method to access the oauth page, I used curl instead :
$request_url = 'https://www.eventbrite.com/oauth/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
I hope it will help anybody encountering the same issue ;)
Just so that this question doesn't continue to show up as unanswered in an auto-email, I'm going to add your answer from your update here -- hope that's alight!
file_get_contents doesn't seem to be a good method to access the oauth page, I used curl instead:
$request_url = 'https://www.eventbrite.com/oauth/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
curl_close($ch);
Thanks for making it easy by answering your own question! ;)

Delete pictures from Facebook using CURL

I have a Facebook app, where I want to delete some pictures. I have the pictureid and access_token, and have tried every methods I have found on this site, but I get errors every time.
How can this be deleted with CURL? I've started with this:
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$uid.'/photos?access_token='.$access_token;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
echo $data = curl_exec($ch);
Applications can now delete Photos - created by that application.
I tried it with a photo uploaded to a page and deleted it with its id via the Graph API Explorer
The curl url would be:
https://graph.facebook.com/'.$imageId.'?method=DELETE&access_token='.$token
Facebook does not permit applications to delete photos at all.

Categories