3
votes

I'm trying to use the Oauth and yt API. I want to try the Browser-based Uploading for youtube. But first I must make so the user can allow the program to have acces to my page. https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading#Browser_based_uploading

But first I need to get an ACCESS_TOKEN, so that why I need a OAuth 2.0 access token. But for some reason I can't get it. What i'm I doing wrong?

So first I go to reddir.php and click on the link so I get redirected to allow the program to use my yt account.

==> Sample post request I need to send

POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf& client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com& client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe& redirect_uri=http://localhost/oauth2callback& grant_type=authorization_code

=> redir.php

<?php   
    $Access_token   =   'https://accounts.google.com/o/oauth2/auth?
                        client_id=xxxxxxxxxxxx.apps.googleusercontent.com&  
                        redirect_uri=http://sdesigns.be/UNSharp/obtain_token.php&
                        scope=https://gdata.youtube.com&
                        response_type=code&
                        access_type=offline';               

    echo '<a href="' . $Access_token . '">Click here to obtain a token2</a>';
?> 

=> obtain_token.php

<?php
           //this file is  
    $client_id = 'xxxxxxxxxxxx.apps.googleusercontent.com';
    $client_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
    $redirect_uri = 'http://sdesigns.be/UNSharp/obtain_token.php';

    $author_code = $_GET["code"];

    echo 'Author code: <br>' .$author_code . '<br>';



    $oauth2token_url = "https://accounts.google.com/o/oauth2/token";
    $clienttoken_post = array(
                            "code" => $code,
                            "client_id" => $client_id,
                            "client_secret" => $client_secret,
                            "redirect_uri" => $redirect_uri,
                            "grant_type" => "authorization_code"
                        );

    $curl = curl_init($oauth2token_url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $json_response = curl_exec($curl);

    echo 'RESULT code: <br>' ;
    var_dump($json_response);
    echo '<br>';    

    curl_close($curl);                                          
 ?>
1
The authorization code expires after a while, and can only be used once. Have you made sure you use a recently generated and unused one?poplitea
yes, its like 5 seconds old or something.user1633254

1 Answers

1
votes

You probably have some encoding issue with your $body2 string, as curl_setopt() is expecting an urlencoded string there.

Just provide the parameters as key-value pairs, as done in this example, so you don't have to care about urlencoding strings:

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);

$curl = curl_init($oauth2token_url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$json_response = curl_exec($curl);
curl_close($curl);