Youtube API V3 - How to get filename from fileDetails - php

I'm brand new to APIs so please excuse what is probably a long and stupid question. Can I modify this code I got from this sample script - https://developers.google.com/youtube/v3/code_samples/php#update_a_video_by_adding_new_tags - to return the original name of the uploaded file for one of my company's videos? Note: I have already modified the linked script to remove the updating of tags (i.e. I just had it print the video id and the current tags to test that I had the Oauth2 working and I did - so, I got that far at least.)
// Call the API's videos.list method to retrieve the video resource.
$listResponse = $youtube->videos->listVideos("snippet",
array('id' => $videoId));
I was thinking that changing to "snippet" to "fileDetails" is what I need. If so, then how do then extract just the original filename from $listResponse?

Welcome to the world of Google Development. Lets look at the documentation a little for videos.list.
If you check under response you will notice that it returns an array of video resources click on that and the documentation will take you to Videos resource representation as you can see there is lots of fun stuff here. Under snippet you find title.
snippet.title string The video's title. The property value has a
maximum length of 100 characters and may contain all valid UTF-8
characters except < and >. You must set a value for this property if
you call the videos.update method and are updating the snippet part of
a video resource.
On the main videos.list documentation page you will find some examples of how to use it with PHP. Here is a hunk of the first one I think you will like.
// Display the list of matching videos.
foreach ($videosResponse['items'] as $videoResult) {
$videos .= sprintf('<li>%s </li>',
$videoResult['snippet']['title']);
}
Googles documentation is very useful always check it.

Related

get filename from google docs using laravel

My site has a feature where users can upload a link to their google docs file. What I want to do is list all the uploaded links in a place. While doing that I need to show the name of the file that is associated with the link.
I can extract the file id from the link and make sure the link is of google docs. Now I need to find a way to get the filename from that. I tried going through the google developer API for google drive, but it is for uploading/doing anything only on the authorized docs. My issue here is, my users upload the files manually to their docs which I have no control over. All I get is a sharable link and somehow get the name out of it. In addition, a thumbnail will also help.
I have tried doing this, but it throws error
$url = "https://www.googleapis.com/drive/v3/files/1G6N6FyXzg7plgEtJn-Cawo5gbghrS8z9_j_cvVqcEDA";
// and
$url = "https://docs.google.com/document/d/1G6N6FyXzg7plgEtJn-Cawo5gbghrS8z9_j_cvVqcEDA/edit?usp=sharing"
$html= file_get_contents($url);
print_r($html);
A dummy link for anyone willing to help: https://docs.google.com/document/d/1G6N6FyXzg7plgEtJn-Cawo5gbghrS8z9_j_cvVqcEDA/edit?usp=sharing
Since we are getting the URL to the file, we can do a couple of things -
get the id to the file
get what type of file is that
Before I explain how to do that, it is better to know that there can be 2 possible situations. One the file is a google docs file, the other google drive file. Those both start with different URLs.
$url = explode('/', Str::after(
Str::after($request->url, 'https://docs.google.com/'), 'https://drive.google.com/'
));
I am using 2 Str::after() to remove the unnecessary part from the URL. Then I am using explode to convert the URL into an array.
Since we have excluded the useless part from the URL, we are left with document/d/1G6N6FyXzg7plgEtJn-Cawo5gbghrS8z9_j_cvVqcEDA/edit?usp=sharing in an array form.
So, if we try to do $url[2], we get the id of the file. Also, "document" is also a good thing to note about. I use those to show proper images. There can be 5 different types of them (4 for google docs and 1 for google drive). Those are - document, spreadsheets, forms, presentation for google docs, and file for google drive. I would recommend everyone store these in the database so that extra calculations are not necessary while displaying it.
Now, to answer the actual part of the question. How to get the name. I have created a new model method to handle that.
public function name()
{
$key = config('app.google_api_key');
$url = "https://www.googleapis.com/drive/v3/files/{$this->file_id}?key={$key}";
$response = Http::get($url)->json();
return $response['name'] ?? 'Private File';
}
Don't forget to add your Google API key in the config file app.php (You need to create one). You can get your API key from Google Developer Console and create a project-specific key. Just to note that this key need not be belonging to the user of the URL.
Also, a thing to note here is that $response returns error code if the file is not set to visible to the public or the file is deleted.

Can't upload or update video with tags

I'm uploading the video through my app using the documented PHP API library with pretty much the same code as here; https://developers.google.com/youtube/v3/code_samples/php#updating_a_video_by_adding_new_tags.
The uploaded video receives the title, description and playlist correctly, but not the tags.
Any clues at all?
Edit:
I'm wondering if it has anything to do with scope, does it matter if i've authorized with https://www.googleapis.com/auth/youtube or https://www.googleapis.com/auth/youtube.upload?
This question: Youtube Google API V3: List Videos not returning video tags
gets an answer suggesting the "onBehalfOfContentOwner" parameter, however, i'm getting "HTTP 403: youtube.common, Access forbidden. The request may not be properly authorized" when trying to upload with that parameter ($insertRequest = $youtube->videos->insert('status,snippet', $video, ['onBehalfOfContentOwner' => true]);)
I found the problem, not sure why it is a problem though; when setting the tags i was doing an array filter, when i removed that as a desperate test or "am i blind and using the wrong array_* function", the tags came through
So changing
$snippet->setTags(array_filter($medium['properties']['keywords']));
to
$snippet->setTags($medium['properties']['keywords']);
did the trick... I could reproduce the problem by re-adding the array_filter, and i made sure to check so it wasn't actually empty,
var_dump(array_filter($medium['properties']['keywords']), $medium['properties']['keywords']);
yielded the same result

D2L Valence - Uploading file to a group locker

My goal is to create a group locker for every person in the class and upload a file to that locker. So first I'm calling
POST /d2l/api/lp/(version)/(orgUnitId)/groupcategories/
in order to create a category. That also creates a group for every user and automatically enrolls them. I then create a locker for each group in the category by calling
POST /d2l/api/lp/(version)/(orgUnitId)/groupcategories/(groupCategoryId)/locker
That works perfectly and at this point every student has it's own group with a locker assigned to the group. It is then up to the file uploading part.
Using the
POST /d2l/api/le/(version)/(orgUnitId)/locker/group/(groupId)/(path)
call I am trying to send a file, but all I get is a 404 error with no response provided. According to D2L's website, 404 would indicate that the group doesn't exists, which is not true since the groupId is received from the first call and works for the second one. For the header I use the array:
$h = array(
'Content-Type: multipart/form-data',
);
The contents are:
$contents = array("FileDescription" => json_encode(
array("Description" => "YY",
"IsPublic" => true
)),
"FILEFILE");
The POST is performed using cURL (php) and the code is mostly the code provided on D2L's website (a modified version of doValenceRequest function). I've tried multiple different headers, as well as different formats for the contents array. No matter what I do, the 404 error is being returned. I've even tried to just create a simple folder instead of uploading a file, but got the same issue. Am I doing something wrong?
Locker file uploads use the simple upload (not resumable) pattern, but, because you send a JSON document along with the file data, you have to use the RFC2388 multipart/mixed pattern, with a POST. See simple upload section on multi-part mixed patterns to see what those HTTP packets looks like.
Of particular note: the HTTP content-type is multipart/mixed and not multipart/form. Also, please be certain that you're actually sending a POST, not a PUT. Some HTTP libraries are pretty finicky around the use of multipart\mixed payloads, and you may find yourself having to hand-cook the entire HTTP request body to get the parts correctly formatted, and tune the headers a bit, before you send the request.
Note also that you should always be sending the JSON document part first in the list of parts, just as in the pattern examples in the documentation.

How do I list all videos uploaded to Youtube?

I'm working with Youtube API and PHP library: http://code.google.com/apis/youtube/2.0/developers_guide_php.html
I'm modifying existing app: http://googlecodesamples.com/youtube/php/YouTubeVideoApp/index.php
That is also available in Zend Gdata library: [no link due to "new users can only post a maximum of two hyperlinks"]
My problem is:
"retrieve my videos" retrieves only 25 and links "back" / "next" are not working :(
The workflow of Youtube API and ZendFramework is really complicated:
index.php calls a javascript function declared in video_app.js
ytVideoApp.listVideos calls ytVideoApp.presentFeed
ytVideoApp.presentFeed sends ajax request operations.php
operations.php strips parameters and calls proper function within itself, that is searchVideos
searchVideos calls echoVideoList
echoVideoList prints the data foreach ($feed as $entry)
ytVideoApp.listVideos updates navigation (back/next buttons)
Existing value: ytVideoApp.MAX_RESULTS_LIST = 200; I also tried to hardcode it, rather than passing via parameter across php, js, ajax. In each case the I receive only 25 results. I tested in on two separate accounts (I do have 2 accounts with more than 25 uploads).
Is there any limitation that remains unknown to me?
Any hints, clues, whatever?
Here is your solution:
http://markmail.org/message/n2px7yheizakzt7y#query:+page:1+mid:vcrj4gmyzfxdtpao+state:results
(click on 'Peter December 5, 2010 - at the top')
As quoted from that link:
Here is the solution that I've come up [with]:
In operations.php find the line: $query->setQuery($searchTerm); and
comment it out.
A few lines below, right under $query->setMaxResults($maxResults);
add this: $query->setAuthor('millenniumpromise');
That's basically it! You can now use the all search type and get
the results published only by millenniumpromise.
I also needed to display the results by published date (most recent
first), so I added $query->setParam('orderby', 'published'); right
below $query->setAuthor('millenniumpromise'); in operations.php
And here is more help from the same players as above:
http://osdir.com/ml/youtube-api-gdata/2010-11/msg00426.html
I must commend Peter and Neil for I have just spent the past week trying to nut this same problem out myself until I found their solution...
Regards,
Adam

Pull twitter profile image

Is there a quick way to pull twitter profile image in PHP or Javascript? I need to get the url of the FULL image (not avatar size). Thanks. Any code sample is good.
Twitter has had a nice simple URL.
https://api.twitter.com/1/users/profile_image/abraham
It has size options like "?size=bigger"
You can read more about it on Little known Twitter and TwitterAPI tips and tricks.
Twitter now has documentation up as GET users/profile_image/:screen_name.
Update: Support for this method has been removed from v1.1 of the API. Recommended practice going forward is GET /users/show and cache profile_image_url locally in your service/app.
function get_big_profile_image($username, $size = '') {
$api_call = 'http://twitter.com/users/show/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
return str_replace('_normal', $size, $results->profile_image_url);
}
get_big_profile_image('bobsaget', '_bigger') should return a large avatar:
http://a1.twimg.com/profile_images/330305510/n229938150541_9850_bigger.jpg
get_big_profile_image('bobsaget') should return an even larger image: http://a1.twimg.com/profile_images/330305510/n229938150541_9850.jpg
Apologies if this is something that's now known, but I didn't see it documented anywhere during my searches, including the official Twitter docs.
You can add the ?size=original as a parameter, which will return the original uploaded image for the user.
So:
http://api.twitter.com/1/users/profile_image/twitter.json?size=original
Previous answerers have provided the correct answer I wanted to link to original twitter api doc page so you'd know it is actually an official way of doing stuff:
You need to specify ?size=
bigger - 73px by 73px
normal - 48px by 48px
mini - 24px by 24px
http://api.twitter.com/1/users/profile_image/twitter.json?size=bigger
http://api.twitter.com/1/users/profile_image/twitter.json?size=normal
http://dev.twitter.com/doc/get/users/profile_image/:screen_name
So, it's not in the docs (http://dev.twitter.com/doc/get/users/profile_image/:screen_name), but it looks like after retrieving the image by specifying any of the three sizes (bigger, normal, mini), you can just remove the suffix before the file extension to get the original image. Hmm... is this safe to use?
For example, this query: api.twitter.com/1/users/profile_image/rrbrambley
Results in: a2.twimg.com/profile_images/931772958/deformed_cropped_headonly_normal.jpg
If I change this url by removing "_normal" then I get the original image: a2.twimg.com/profile_images/931772958/deformed_cropped_headonly.jpg
I know there are apps that use the original image. This must be the way?
When you get original image link, you can modify it to get bigger.
http://pbs.twimg.com/profile_images/34543543/image_name_normal.jpg
becomes
http://pbs.twimg.com/profile_images/34543543/image_name.jpg or image_name_bigger, ...
Source: https://dev.twitter.com/docs/user-profile-images-and-banners
I know this isn't the full code sample as requested (because there are several ways of doing this), but do you already have the URL for the avatar? I noticed that turning ".../eric.png" into ".../eric_bigger.png" resulted in the larger image. When "_bigger" already exists, removing it gave me the URL to the original image.
I tested this with several followers' profile images and, when the profile image was > 150px square, worked.
Twitter profile images urls:
Bigger: https://api.twitter.com/1/users/profile_image/puneetsindhwani/?size=bigger
Original: https://api.twitter.com/1/users/profile_image/puneetsindhwani/?size=original

Categories