Skip to content

Commit

Permalink
Merge branch 'remove-scopes' into rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiob committed May 16, 2014
2 parents 031a0a3 + 8bceaa6 commit c895a0e
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
yt (0.5.2)
yt (0.5.3)
activesupport

GEM
Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Expand Up @@ -6,6 +6,7 @@ v0.5 - 2014/05/16
* Allow resources to be initialized with a url, such as Yt::Resource.new url: 'youtube.com/fullscreen'
* Add `has_one :id` to resources, to retrieve the ID of resources initialized by URL
* Raise an error if some `has_one` associations are not found (id, snippet, details set, user info)
* Don't check for the right :scope if Account is initialized with credentials

v0.4 - 2014/05/09
--------------------
Expand Down
10 changes: 7 additions & 3 deletions README.md
Expand Up @@ -302,7 +302,7 @@ To install on your system, run

To use inside a bundled Ruby project, add this line to the Gemfile:

gem 'yt', '~> 0.5.2'
gem 'yt', '~> 0.5.3'

Since the gem follows [Semantic Versioning](http://semver.org),
indicating the full version in your Gemfile (~> *major*.*minor*.*patch*)
Expand Down Expand Up @@ -389,8 +389,12 @@ export YT_TEST_DEVICE_CLIENT_SECRET="1234567890"
export YT_TEST_SERVER_API_KEY="123456789012345678901234567890"
```

[ TODO: Complete this section. Explain how get and store the refresh token,
making sure all the required scopes are authorized. ]
[ TODO:
Complete this section.
Explain how get and store the refresh token, making sure all the required scopes are authorized:
'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
]



How to contribute
Expand Down
8 changes: 6 additions & 2 deletions TODO.md
@@ -1,3 +1,9 @@
* methods like Yt::Account.new(params = {}) should use HashWithIndifferentAccess
* add canonical_url to Resource, then use it in promo

* once in a while, Google fails with 500 error and just retrying after some
seconds fixes it, so we should retry every 500 at least

* find by url (either video or channel or.. playlist)
* Google accounts?
* ENV support
Expand All @@ -7,5 +13,3 @@
* operations like subscribe that require authentication should not fail if
called on Yt::Channel without auth but, similarly to account, show the prompt
or ask for the device code

* scope
1 change: 0 additions & 1 deletion lib/yt/collections/details_sets.rb
Expand Up @@ -14,7 +14,6 @@ def new_item(data)
def list_params
super.tap do |params|
params[:params] = {maxResults: 50, part: 'contentDetails', id: @parent.id}
params[:scope] = 'https://www.googleapis.com/auth/youtube.readonly'
params[:path] = '/youtube/v3/videos'
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/yt/collections/playlist_items.rb
Expand Up @@ -29,7 +29,6 @@ def new_item(data)
def list_params
super.tap do |params|
params[:params] = {maxResults: 50, part: 'snippet,status', playlistId: @parent.id}
params[:scope] = 'https://www.googleapis.com/auth/youtube.readonly'
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/yt/collections/playlists.rb
Expand Up @@ -32,7 +32,6 @@ def new_item(data)
def list_params
super.tap do |params|
params[:params] = {maxResults: 50, part: 'snippet,status', channelId: @parent.id}
params[:scope] = 'https://www.googleapis.com/auth/youtube.readonly'
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/yt/collections/snippets.rb
Expand Up @@ -15,7 +15,6 @@ def list_params
parents_path = @parent.class.to_s.demodulize.underscore.pluralize
super.tap do |params|
params[:params] = {id: @parent.id, part: 'snippet'}
params[:scope] = 'https://www.googleapis.com/auth/youtube.readonly'
params[:path] = "/youtube/v3/#{parents_path}"
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/yt/collections/user_infos.rb
Expand Up @@ -14,8 +14,6 @@ def new_item(data)
def list_params
super.tap do |params|
params[:path] = '/oauth2/v2/userinfo'
# TODO: Remove youtube from here, implement incremental scopes
params[:scope] = 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
end
end

Expand Down
1 change: 0 additions & 1 deletion lib/yt/collections/videos.rb
Expand Up @@ -14,7 +14,6 @@ def new_item(data)
def list_params
super.tap do |params|
params[:params] = {channelId: @parent.id, type: :video, maxResults: 50, part: 'snippet'}
params[:scope] = 'https://www.googleapis.com/auth/youtube.readonly'
params[:path] = '/youtube/v3/search'
end
end
Expand Down
18 changes: 1 addition & 17 deletions lib/yt/models/account.rb
Expand Up @@ -10,28 +10,12 @@ class Account < Base
:name, :given_name, :family_name, :profile_url, :avatar_url, :locale, :hd]

def initialize(options = {})
# By default is someone passes a refresh_token but not a scope, we can assume it's a youtube one
@scope = options.fetch :scope, 'https://www.googleapis.com/auth/youtube'
@access_token = options[:access_token]
@refresh_token = options[:refresh_token]
@redirect_url = options[:redirect_url]
end

def access_token_for(scope)
# TODO incremental scope

# HERE manage the fact that we must change some scope on device,
# like 'https://www.googleapis.com/auth/youtube.readonly' is not accepted
if Yt.configuration.scenario == :device_app && scope == 'https://www.googleapis.com/auth/youtube.readonly'
scope = 'https://www.googleapis.com/auth/youtube'
end

# TODO !! include? is not enough, because (for instance) 'youtube' also includes 'youtube.readonly'

# unless (@scope == scope) || (scope == 'https://www.googleapis.com/auth/youtube.readonly' && @scope =='https://www.googleapis.com/auth/youtube')
# @scope = scope
# @access_token = @refresh_token = nil
# end
def access_token
@access_token ||= refresh_access_token || get_access_token
end

Expand Down
6 changes: 2 additions & 4 deletions lib/yt/models/request.rb
Expand Up @@ -14,7 +14,6 @@ def initialize(options = {})
@uri = URI::HTTPS.build options.slice(:host, :path, :query)
@method = options.fetch :method, :get
@format = options[:format]
@scope = options[:scope]
@body = options[:body]
@body_type = options[:body_type]
@auth = options[:auth]
Expand All @@ -36,16 +35,15 @@ def self.default_params
{}.tap do |params|
params[:format] = :json
params[:host] = 'www.googleapis.com'
params[:scope] = 'https://www.googleapis.com/auth/youtube'
params[:body_type] = :json
end
end

private

def add_authorization_to_request!
if @auth.respond_to? :access_token_for
@headers['Authorization'] = "Bearer #{@auth.access_token_for @scope}"
if @auth.respond_to? :access_token
@headers['Authorization'] = "Bearer #{@auth.access_token}"
elsif Yt.configuration.api_key
params = URI.decode_www_form @uri.query || ''
params << [:key, Yt.configuration.api_key]
Expand Down
2 changes: 1 addition & 1 deletion lib/yt/version.rb
@@ -1,3 +1,3 @@
module Yt
VERSION = '0.5.2'
VERSION = '0.5.3'
end

0 comments on commit c895a0e

Please sign in to comment.