Skip to content

How To: Use OAuth 2

Benjamin Lieb edited this page Feb 28, 2017 · 9 revisions

OAUTH 2 WITH YOUTUBE_IT GEM

If you want to use youtube_it with OAUTH2 first you need to create a new project here

https://code.google.com/apis/console

after register your app you need to get the following data steps for register app

client_id
client_secret

then you have to go to this url for get the token

* this is for Server-side web applications, you can see more here

_* this link is currently (Feb 2017) producing an error, due to the deprecation of gdata. Not sure what the fix is...

https://accounts.google.com/o/oauth2/auth?
  client_id=68330730158.apps.googleusercontent.com&
  redirect_uri=https://youtube-it.heroku.com/&
  scope=https://gdata.youtube.com&
  response_type=code&
  approval_prompt=force&
  access_type=offline

then with the returned token, you have to make a post request like this

require 'rubygems'
require 'faraday'

 conn = Faraday.new(:url => 'https://accounts.google.com',:ssl => {:verify => false}) do |faraday|
   faraday.request  :url_encoded
   faraday.response :logger
   faraday.adapter  Faraday.default_adapter
  end

 result = conn.post '/o/oauth2/token', {'code' => "your token",
 'client_id' => "your client id",
 'client_secret' => "your client secret",
 'redirect_uri' => "your redirect url",
 'grant_type' => 'authorization_code'}

 puts result.body.inspect

and this ruby code returns

{
  "access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}

then you just need initialize the client youtube_it

client = YouTubeIt::OAuth2Client.new(client_access_token: "your access token", client_id: "client id app", client_secret: "client secret app", dev_key: "dev key")

* YouTube Api OAUTH 2 Documentation

Clone this wiki locally