Skip to content

Instantly share code, notes, and snippets.

@Remiii
Last active December 29, 2015 15:19
Show Gist options
  • Save Remiii/a0bb9e1d346864f64354 to your computer and use it in GitHub Desktop.
Save Remiii/a0bb9e1d346864f64354 to your computer and use it in GitHub Desktop.
Google Cloud: Generating a refresh token

GENERATING A REFRESH TOKEN

STEP 1

  • Go to http://cloud.google.com/console and log-in
    • Activate youtube-data API (APIs & auth -> APIs)
    • Register a new app (APIs & auth -> registered apps)
      • Name it, select native
      • Write down your client-id and client-secret

Marks: https://developers.google.com/console/help/new

  • Set $client_id and in step1_url.php and step2_refresh_token.php

  • $ php step1_url.php and open the url in your browser

    • Write down the auth code ('code' in step2_refresh_token.php).
  • Set 'client_secret' and 'code' in step2_refresh_token.php

  • $ php step2_refresh_token.php

    • Write down your refresh token
  • Save your client-id, client-secret and refresh-token

See more info on this thread: http://stackoverflow.com/questions/8257678/google-calendar-api-v3-hardcoded-credentials

See more info about client scret: http://stackoverflow.com/questions/15547019/how-do-i-find-the-googles-oauth-2-0-client-secret-key-for-developing-chrome-ext

#!/usr/bin/php
<?php
$client_id = 'yourClientId' ;
$params = array(
'response_type' => 'code',
'client_id' => $client_id,
'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob',
'scope' => 'https://www.googleapis.com/auth/youtube.upload',
) ;
$url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params) ;
echo $url . "\n" ;
?>
#!/usr/bin/php
<?php
$url = 'https://accounts.google.com/o/oauth2/token' ;
$post_data = array(
'code' => 'yourAuthCode',
'client_id' => 'yourClientId',
'client_secret' => 'yourClientSecret',
'redirect_uri' => 'urn:ietf:wg:oauth:2.0:oob',
'grant_type' => 'authorization_code',
) ;
$ch = curl_init() ;
curl_setopt($ch, CURLOPT_URL, $url) ;
curl_setopt($ch, CURLOPT_POST, 1) ;
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data) ;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
$result = curl_exec($ch) ;
$token = json_decode($result) ;
print_r($token) ;
?>
@Remiii
Copy link
Author

Remiii commented May 10, 2015

After that you can get AccessToken with code like this!

    private function getAccessToken($app)
    {
        $token_url = 'https://accounts.google.com/o/oauth2/token';
        $post_data = array(
            'client_secret' =>   $app['google']['client_secret'],
            'grant_type'    =>   'refresh_token',
            'refresh_token' =>   $app['google']['refresh_token'],
            'client_id'     =>   $app['google']['client_id']
        );
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $token_url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        $token_object = json_decode($result);
        return $token_object->access_token;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment