I'm fairly new to Stack Overflow, so if I am doing something wrong please tell me;
I have setup my website with the Google+ API - to log in, people click on a "Login With Google+" button and log in.
That's all fine and good, but I was wondering if I can integrate this login process with the YouTube API, so that when the user clicks the button to log in, I can also gather YouTube information.
I've already setup the YouTube Analytics and Data APIs on the Google API Console, I just need a code snippet to store the YouTube Username.
Expected Behavior:
User with URL 'youtube.com/some-user' logs in with Google+
Google+ Authentication Key is fed into some script
Script connects to YouTube API and returns username (eg $youtube_user = 'some-user')
My website takes this information and continues with the script
You can do this using the YouTube API. What you need to do is add an additional scope to your sign-in button. I would recommend you use the following scope:
https://www.googleapis.com/auth/youtube.readonly
Do this because you will not be managing anyone's YouTube account, just seeing their personal data (e.g. to pull in links or embed code for their YouTube videos).
Next, you need to perform an API call to YouTube using a client library. For experimentation, you can use the API explorer. To see your own information, authorize the request, set the part to "snippet" (no quotes) and then set mine to true.
Download the "youtube", "v3" static class from the PHP Google API client library, then make a call passing snippet as the part you want and "mine" set to true.
For listing the current user's videos, there is an example that ships with the PHP client library. The following change would list the user's activities:
// Exchange the OAuth 2.0 authorization code for user credentials.
$client->authenticate($code);
$token = json_decode($client->getAccessToken());
...
$activities = $youtube->activities->listActivities('snippet', array(
'mine' => 'true',
));
foreach ($activities['items'] as $activity) {
error_log(serialize($activity));
}
class's answer is spot on in terms of getting the OAuth 2 credentials to make the request. In terms of actual PHP code to work off of, you can use this example as a starting point (which uses this client library), and modify line 40 to read
$channelsResponse = $youtube->channels->listChannels('snippet', array(
'mine' => 'true',
));
And then you can access the channel's title via $channelsResponse['items'][0]['snippet']['title']. Note that it's possible to link a Google+ account and a YouTube channel, and if that's done, the channel's title will be equal to the Google+ account's display name. If this is an unlinked YouTube channel, then the legacy username of the channel will be returned instead.
Related
I have a website on which clients will be uploading videos. The thing is - I want those videos to be uploaded to OUR channel, not users' channels. I don't see how I could make it work using YouTube API v3.
Every time a person wants to upload something using the access token I provided he has to input his own credentials. He can't simply upload anything directly to OUR channel.
Is there a way to work that around?
Short answer is: You should not ask your users to upload into your account.
But please read further:
https://stackoverflow.com/a/12626209/1973552
https://stackoverflow.com/a/15258781/1973552
Api V2 have option you've mentioned. Did you try it?
Basically you want to let Users upload Videos in your website, but upload them directly to youtube, without Google AUTH?
Yes you can. Make sure you set the setAccessType to offline.
$client->setAccessType("offline");
This will make it so you get a refresh Token with the users account authentication the first time. A refresh token allows you to reauthenticate without the user being present. (aka, offline). Create your script from the sample API v3 scripts, authorize your main account (the one you want to be predetermined) and display the response. Something like the following will show you your response:
print_r($client->getAccessToken());
You will see your refresh Token in the json response. Save that (either to a database, or hard code it into the script). Now that you have a refresh token, whenever you want to upload to youtube, just call
$client->refreshToken($THE_REFRESH_TOKEN_YOU_SAVED);
$thetoken = $client->getAccessToken();
$client->setAccessToken($thetoken);
After this, your previously authenticated account will now be currently logged into your script.
FYI, these are php examples, you might need to adjust accordingly.
I am using Google Youtube API and YoutubeAnalytics API, I read on their documentation that you need a CHANNEL_ID inorder for you to grab the user's videos.
I can now successfully Google authenticate the user in my application, I just wanted to ask if how can I get their CHANNEL_ID after they authenticate.
I am using PHP, I've been searching over the net but can't seem to find any example how to do that.
Your help will be greatly appreciated!
Thanks! :)
Since you're using PHP, I'm assuming you're using the PHP-based gapi-client? Here's the call to make:
$listResponse = $youtube->channels->listChannels('id', array(
'mine' => 'true',
))
The 'id' part returns the ID of the channel, and by passing the parameter "mine" as true, you're telling the API to return the data for the authenticated user.
If you're using a different client or rolled your own, you can also just hit this endpoint (passing the oAuth access token you have in the header or as another parameter to the request):
GET https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&key={YOUR_API_KEY}
How can I "link" a person's youtube account to an account on my website? I am trying to get Analytics from videos, how much money they have made, etc. I know i am supposed to be using the YouTube Analytics API, but I see tons of different documentation and it gets SO confusing. Are there any PHP libraries I can use to get this data and to link the user's account to my web application? I am also confused on where I get an OAuth Key.
Here are some sites i have looked at:
1) Site One
2) Site Two
On site two, I looked at the examples, but nothing really helped me understand even how to start.
A lot of the relevant info you'll need can be found in this document:
https://developers.google.com/youtube/analytics/authentication
Basically, it outlines the following 4 steps:
1) Register your web app in the Google Cloud Console
This is needed so you can get a client secret and client ID, which your server-side PHP code will need in order to do the oAuth flow (and get the right scope to be able to query analytics data for the user that's authenticating). See here for more info on how to do this:
https://developers.google.com/youtube/analytics/registering_an_application
The most important things to do as your register your app are to turn on the YouTube Analytics API and create a new client ID for your web application.
2) When a user visits your page, you'll need some way (i.e. a login button, for example) to trigger the start of the oAuth flow. When this is triggered, you'll want to redirect the browser to this URL:
https://accounts.google.com/o/oauth2/auth?client_id=[YOUR CLIENT ID]&redirect_uri=[THE URL YOU WANT THE USER TO BE DIRECTED TO AFTER AUTHENTICATION]&scope=https://www.googleapis.com/auth/yt-analytics.readonly&response_type=code&access_type=offline
This will present them with a window asking them if they want to give permission to your app to read their analytics. Note that the client id parameter is the same that you received when you registered your app in step 1. That registration process also will require you to set the allowed redirect URIs, so here you must pass one you set in the registration.
3) The redirect URL will be requested, from step two, by Google's servers with a "code" parameter attched. So when it is requested, it should immediately do a POST to another URL (i.e. with cURL or something similar), that looks like this:
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=[CODE THAT CAME IN AS A GET PARAMETER] &client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&redirect_uri=[THE REGISTERED REDIRECT URI]&grant_type=authorization_code
If you do it as a POST with cURL, then the response will be a JSON packet that has an access token and a refresh token.
4) Your php page can store these both (in your DB, for example), note that the user should be treated as logged in at this point, and you can use the access token in the header of all API requests send to the analytics API.
https://developers.google.com/youtube/analytics/authentication#OAuth2_Calling_a_Google_API
IT'll expire in an hour, so with each request you should be checking its age (i.e. when you stored it in the DB, you could store the expiry time, for example), and when you're getting close you can use the refresh token to get a new access token.
https://developers.google.com/youtube/analytics/authentication#OAuth2_Refreshing_a_Token
You can now redirect them to wherever your app needs them to be to start interfacing with the API.
Seems like a lot? It can be, but once you get the paradigm down it's pretty simple. And you asked about a client for PHP, and thankfully there is one:
https://github.com/google/google-api-php-client
It's got simple handlers for the whole oAuth2 flow, and also has a YouTube analytics service object that sets the access token automatically for you as it's making its various calls.
I have a site and I want to upload videos onto YouTube without a login. Is it possibe? If yes, how can do this?
Create an account and use its credentials all the time, for all users of your site. You simply can't upload a video without a user account 'responsible' for it. #Pekka: ask for forgiveness, not for permission? ;)
Google at least has a youtube API (with uploading capabilities and PHP examples) right here: http://code.google.com/apis/youtube/2.0/developers_guide_php.html#Uploading_Videos
It is not possible to upload Video to YouTube without logging in.
That said, I wouldn't be surprised if even automated uploading with a login would be forbidden by YouTube's Terms and Conditions.
There is a way to do that without zend client library. Its in core php (PHP4).
https://github.com/techie28/YouTubeUploadPHP.
Note: AuthSub is deprecated now.Please refer to Google Deprecated policy for details.
EDIT:
Because codershelpingcoders.com now points to godaddy's parking page and the original link zendtutorials.wordpress.com has an empty article linking to codershelpingcoders.com, I found the original article via the archive: http://web.archive.org/web/20130123044500/http://codershelpingcoders.com/ and have tried to replicate it's contents in this answer for future reference (NOTE: I have no idea if this info still works).
This tutorial describes the direct browser based upload technique using AuthSub.
AuthSub is the Authorization module of the YouTube that lets your application interact with the YouTube for specific purposes such as Uploading videos etc on user’s behalf.
It is same like Auth and a cousin of oAuth.
A user grants the privilege to your site application and you can do the job on his behalf as simple as that.
We will go through the way to upload a video using AuthSub.
It goes as follows and can be really done in following 4 simple steps:
To allow the application run on user behalf a user must have
authorized it first.
So our first step to implement is to get the app Authorized by the user.
We do it by simply redirecting user to the authorization page the url
is as follows:
$nextUrl = urlencode(‘http://www.xxxx.com’)
$scope = urlencode(‘http://gdata.youtube.com’);
https://www.google.com/accounts/AuthSubRequest?next=’.$nextUrl.’&scope=’.$scope.’&session=1&secure=0
The nextUrl here is the url of the your application where the user
will be redirected after authorization procedure.
scope is to tell the YouTube about the scope of the process which is google
data youtube in this case.
So if user has not authorized your app yet he must be redirected to
the above mentioned authorization page once the user has approved
your application it needs not to follow the step one ever again until
and unless the user revokes the access to you app from the users
control panel of his account.
On successful completion of the authorization process user will be
redirected to your application and this complete the first step of
AuthSub.
If from the first step the user authenticates your application
YouTube will redirect him back to your application with a token in the url.
You are going to use this token and here is where the actual AuthSub process
comes into play you are going to use this token to obtain an entity called
AuthSubSessionToken which will allow you to interact your app to YouTube
on the user behalf who has just approved your application.
In PHP you do it by issuing a curl request. The details are as follows:
Issue a curl GET request to https://www.google.com/accounts/AuthSubSessionToken
with the token you received just after the authorization step.
Remember to turn ON the curl’s response gathering status as you gonna need that.
If everything went well till now you would be responded from YouTube with
the AuthSubSessionToken.
BINGO :-)
Now when you have received the AuthSubSessionToken you are gonna use
that to get an upload token which will actually upload the data
related to your video to YouTube i.e.title,description,category and
keywords. This is kinda reverse process as in AuthSub you upload the
data related to the video to YouTube first and then upload the video
itself. The uploading of video data also referred as MetaData will be
done by feeding XML to the YouTube,the xml will be:
title goes here
description goes here
category goes here
Keyword goes here
and again curl has business to do you will upload this by issuing another curl call:
url:http://gdata.youtube.com/action/GetUploadToken
headers:AuthSub token=”Your AuthSubSession token goes here”
GData-Version:2
‘X-GData-Key: key=”Your Api key goes here”
Content-length: length of the xml you formed above goes here
Content-Type:application/atom+xml; charset=UTF-8
POSTFIELDS: the xml itself that you formed
If the step 3 completes successfully then its time to upload the
video actually on your successful last curl execution you will be
reverted back by the YouTube with a url an a token.
Now you will create a form which will have this url as its action and token
as a hidden field something like this.
Just select the video and click submit and your video will get uploaded.
On successful submission you will be redirected back with status 200.
The github link for the Sample code is here.
I am trying to do # mentions on Facebook using either facebook.stream.publish or facebook.users.setStatus.
You are able to do this on Facebook and I know its possible to do using 3rd party APIs as a "Twitter Sync" app currently supports it.
So far I have tried using: #[fb_id], #:fb_id, #[fb_id:fb_name], #:[fb_id:fb_name] and #:[fb_id] in the stream message.
I know Facebook.com uses #[fb_id:fb_name] on the site.
I also know Facebook's Dashboard API uses #:fb_id
I am also willing to emulate this functionality on my end if someone knows how to insert an url into the user's status or stream with custom link text or some method of inserting FBML into the stream (<fb:name>)
Note:
fb_id = Facebook User ID
fb_name = Facebook User Name
example: #[552192373:Jason Boehm]
I am currently using Facebook's Offical PHP API Client.