forked from youtube/api-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add file for coming-soon Ruby quickstart guide.
- Loading branch information
1 parent
55bf401
commit 53c6d50
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Sample Ruby code for user authorization | ||
|
||
require 'rubygems' | ||
gem 'google-api-client', '>0.7' | ||
require 'google/apis' | ||
require 'google/apis/youtube_v3' | ||
require 'googleauth' | ||
require 'googleauth/stores/file_token_store' | ||
|
||
require 'fileutils' | ||
require 'json' | ||
|
||
# REPLACE WITH VALID REDIRECT_URI FOR YOUR CLIENT | ||
REDIRECT_URI = 'http://localhost' | ||
APPLICATION_NAME = 'YouTube Data API Ruby Tests' | ||
|
||
# REPLACE WITH NAME/LOCATION OF YOUR client_secrets.json FILE | ||
CLIENT_SECRETS_PATH = 'client_secret.json' | ||
|
||
# REPLACE FINAL ARGUMENT WITH FILE WHERE CREDENTIALS WILL BE STORED | ||
CREDENTIALS_PATH = File.join(Dir.home, '.credentials', | ||
"youtube-quickstart-ruby-credentials.yaml") | ||
|
||
# SCOPE FOR WHICH THIS SCRIPT REQUESTS AUTHORIZATION | ||
SCOPE = Google::Apis::YoutubeV3::AUTH_YOUTUBE_READONLY | ||
|
||
def authorize | ||
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH)) | ||
|
||
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH) | ||
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH) | ||
authorizer = Google::Auth::UserAuthorizer.new( | ||
client_id, SCOPE, token_store) | ||
user_id = 'default' | ||
credentials = authorizer.get_credentials(user_id) | ||
if credentials.nil? | ||
url = authorizer.get_authorization_url(base_url: REDIRECT_URI) | ||
puts "Open the following URL in the browser and enter the " + | ||
"resulting code after authorization" | ||
puts url | ||
code = gets | ||
credentials = authorizer.get_and_store_credentials_from_code( | ||
user_id: user_id, code: code, base_url: REDIRECT_URI) | ||
end | ||
credentials | ||
end | ||
|
||
# Initialize the API | ||
service = Google::Apis::YoutubeV3::YouTubeService.new | ||
service.client_options.application_name = APPLICATION_NAME | ||
service.authorization = authorize | ||
|
||
# Sample ruby code for channels.list | ||
|
||
def channels_list_by_username(service, part, **params) | ||
response = service.list_channels(part, params).to_json | ||
item = JSON.parse(response).fetch("items")[0] | ||
|
||
puts ("This channel's ID is #{item.fetch("id")}. " + | ||
"Its title is '#{item.fetch("snippet").fetch("title")}', and it has " + | ||
"#{item.fetch("statistics").fetch("viewCount")} views.") | ||
end | ||
|
||
channels_list_by_username(service, 'snippet,contentDetails,statistics', for_username: 'GoogleDevelopers') |