Skip to content

Commit ddfe916

Browse files
authored
Use new oauth2 endpoints (#422)
* Update oauth2 endpoints * Fix test
1 parent c4c005c commit ddfe916

20 files changed

+158
-118
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ doc/
2525
.yardoc/
2626
_site/
2727
TODO.md
28-
29-
spec/cassettes

lib/yt/associations/has_authentication.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def initialize(options = {})
3232
@force = options[:force]
3333
@scopes = options[:scopes]
3434
@authentication = options[:authentication]
35+
@state = options[:state]
3536
end
3637

3738
def auth
@@ -48,7 +49,7 @@ def authentication
4849

4950
def authentication_url
5051
host = 'accounts.google.com'
51-
path = '/o/oauth2/auth'
52+
path = '/o/oauth2/v2/auth'
5253
query = authentication_url_params.to_param
5354
URI::HTTPS.build(host: host, path: path, query: query).to_s
5455
end
@@ -190,6 +191,7 @@ def authentication_url_params
190191
params[:access_type] = :offline
191192
params[:approval_prompt] = @force ? :force : :auto
192193
# params[:include_granted_scopes] = true
194+
params[:state] = @state if @state
193195
end
194196
end
195197

@@ -243,4 +245,4 @@ def client_secret
243245
end
244246
end
245247
end
246-
end
248+
end

lib/yt/collections/authentications.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def attributes_for_new_item(data)
1616

1717
def list_params
1818
super.tap do |params|
19-
params[:host] = 'accounts.google.com'
20-
params[:path] = '/o/oauth2/token'
19+
params[:host] = 'oauth2.googleapis.com'
20+
params[:path] = '/token'
2121
params[:request_format] = :form
2222
params[:method] = :post
2323
params[:auth] = nil

lib/yt/collections/device_flows.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ def attributes_for_new_item(data)
1515

1616
def list_params
1717
super.tap do |params|
18-
params[:path] = '/o/oauth2/device/code'
18+
params[:host] = 'oauth2.googleapis.com'
19+
params[:path] = '/device/code'
1920
end
2021
end
2122
end
2223
end
23-
end
24+
end

lib/yt/collections/revocations.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ def attributes_for_new_item(data)
1515

1616
def list_params
1717
super.tap do |params|
18-
params[:host] = 'accounts.google.com'
19-
params[:path] = '/o/oauth2/revoke'
20-
params[:request_format] = nil
21-
params[:method] = :get
18+
params[:host] = 'oauth2.googleapis.com'
19+
params[:path] = '/revoke'
20+
params[:request_format] = :form
21+
params[:method] = :post
2222
params[:auth] = nil
2323
params[:body] = nil
2424
params[:camelize_body] = false

lib/yt/collections/user_infos.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UserInfos < Base
1313
# @see https://developers.google.com/+/api/latest/people/getOpenIdConnect
1414
def list_params
1515
super.tap do |params|
16-
params[:path] = '/oauth2/v2/userinfo'
16+
params[:path] = '/oauth2/v3/userinfo'
1717
params[:expected_response] = Net::HTTPOK
1818
end
1919
end
@@ -33,4 +33,4 @@ def next_page
3333
end
3434
end
3535
end
36-
end
36+
end

lib/yt/models/statistics_set.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ def initialize(options = {})
1717
has_attribute :view_count, type: Integer
1818
has_attribute :comment_count, type: Integer
1919
has_attribute :like_count, type: Integer
20+
21+
# statistics.dislikeCount property included in an API response
22+
# only if the API request was authenticated by the video owner
23+
# as of December 13, 2021
2024
has_attribute :dislike_count, type: Integer
25+
2126
has_attribute :favorite_count, type: Integer
2227
has_attribute :video_count, type: Integer
2328
has_attribute :subscriber_count, type: Integer

spec/requests/as_account/account_spec.rb

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
let(:count) { test_account.videos.where(q: query).count }
4343

4444
context 'given a query string that matches any video owned by the account' do
45-
let(:query) { 'coffee' }
45+
let(:query) { 'Test' }
4646
it { expect(count).to be > 0 }
4747
end
4848

@@ -90,24 +90,27 @@
9090
end
9191
end
9292

93-
describe '.video_groups' do
94-
let(:video_group) { test_account.video_groups.first }
95-
before do
96-
allow(Date).to receive(:today).and_return(Date.new(2020, 2, 12))
97-
end
98-
99-
specify 'returns the first video-group created by the account' do
100-
expect(video_group).to be_a Yt::VideoGroup
101-
expect(video_group.title).to be_a String
102-
expect(video_group.item_count).to be_an Integer
103-
expect(video_group.published_at).to be_a Time
104-
end
105-
106-
specify 'allows to run reports against each video-group' do
107-
expect(video_group.views).to be
108-
end
109-
end
110-
93+
# TODO: uncomment video_groups test and run when we have a test account with
94+
# video groups. (I think only cms can add a video group)
95+
# describe '.video_groups' do
96+
# let(:video_group) { test_account.video_groups.first }
97+
# before do
98+
# allow(Date).to receive(:today).and_return(Date.new(2020, 2, 12))
99+
# end
100+
101+
# specify 'returns the first video-group created by the account' do
102+
# expect(video_group).to be_a Yt::VideoGroup
103+
# expect(video_group.title).to be_a String
104+
# expect(video_group.item_count).to be_an Integer
105+
# expect(video_group.published_at).to be_a Time
106+
# end
107+
108+
# specify 'allows to run reports against each video-group' do
109+
# expect(video_group.views).to be
110+
# end
111+
# end
112+
113+
# FYI: there's a limit to upload videos, once hit the limit, need wait 24 hours
111114
describe '.upload_video' do
112115
let(:video_params) do
113116
{title: 'Test Yt upload', privacy_status: 'private', category_id: 17, self_declared_made_for_kids: true}

spec/requests/as_account/channel_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,14 @@
116116
# end
117117
end
118118

119-
specify 'with a public list of subscriptions' do
120-
expect(channel.subscribed_channels.first).to be_a Yt::Channel
121-
end
119+
# NOTE: API doesn't grant access to the subscriptions of other users
120+
# any more.
121+
# specify 'with a public list of subscriptions' do
122+
# expect(channel.subscribed_channels.first).to be_a Yt::Channel
123+
# end
122124

123125
context 'with a hidden list of subscriptions' do
124-
let(:id) { 'UCUZHFZ9jIKrLroW8LcyJEQQ' } # YouTube Creators - better make our own one
126+
# let(:id) { 'UCUZHFZ9jIKrLroW8LcyJEQQ' } # YouTube Creators
125127
it { expect{channel.subscribed_channels.size}.to raise_error Yt::Errors::Forbidden }
126128
end
127129

spec/requests/as_account/channels_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
context 'with a list of parts' do
99
let(:part) { 'statistics' }
10-
let(:channel) { channels.where(part: part, id: 'UCZDZGN_73I019o6UYD2-4bg').first }
10+
let(:channel) { channels.where(part: part, id: 'UCBR8-60-B28hp2BmDPdntcQ').first } # YouTube
1111

1212
specify 'load ONLY the specified parts of the channels' do
1313
expect(channel.instance_variable_defined? :@snippet).to be false

spec/requests/as_account/playlist_item_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
subject(:item) { Yt::PlaylistItem.new id: id, auth: test_account }
66

77
context 'given an existing playlist item' do
8-
let(:id) { 'UExiai1JRGU2Zzh2dGF4MVNha0xFd09kT0V2LW52aml5MC41NkI0NEY2RDEwNTU3Q0M2' }
8+
let(:id) { 'UEx3akRSUDVaWF9NRnNldlI4ZGlRMmx2M0hiTW81eGJLYy41NkI0NEY2RDEwNTU3Q0M2' }
99

1010
it 'returns valid metadata' do
1111
expect(item.title).to be_a String
@@ -23,7 +23,7 @@
2323
end
2424

2525
context 'given one of my own playlist items that I want to update' do
26-
let(:id) { 'UExiai1JRGU2Zzh2dGF4MVNha0xFd09kT0V2LW52aml5MC41NkI0NEY2RDEwNTU3Q0M2' }
26+
let(:id) { 'UEx3akRSUDVaWF9NRnNldlI4ZGlRMmx2M0hiTW81eGJLYy41MjE1MkI0OTQ2QzJGNzNG' }
2727
let!(:old_title) { item.title }
2828
let!(:old_privacy_status) { item.privacy_status }
2929
let(:update) { item.update attrs }

spec/requests/as_account/playlist_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@
6565
end
6666

6767
context 'given one of my own playlists that I want to delete' do
68-
let(:id) { 'PLbj-IDe6g8vuN_pRohu-634bvJKZzxWht' }
68+
let(:id) { 'PLwjDRP5ZX_MFsevR8diQ2lv3HbMo5xbKc' }
6969

7070
it { expect(playlist.delete).to be true }
7171
end
7272

7373
context 'given one of my own playlists that I want to update' do
74-
let(:id) { 'PLbj-IDe6g8vtax1SakLEwOdOEv-nvjiy0' }
74+
let(:id) { 'PLwjDRP5ZX_MFsevR8diQ2lv3HbMo5xbKc' }
7575
let!(:old_title) { playlist.title }
7676
let!(:old_privacy_status) { playlist.privacy_status }
7777
let(:update) { playlist.update attrs }

spec/requests/as_account/video_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
expect(video.live_broadcast_content).to be_a String
2626
expect(video.view_count).to be_an Integer
2727
expect(video.like_count).to be_an Integer
28-
expect(video.dislike_count).to be_an Integer
28+
# expect(video.dislike_count).to be_an Integer
2929
expect(video.favorite_count).to be_an Integer
3030
expect(video.comment_count).to be_an Integer
3131
expect(video.duration).to be_an Integer
@@ -92,7 +92,7 @@
9292
end
9393

9494
context 'given someone else’s live video broadcast scheduled in the future' do
95-
let(:id) { 'PqzGI8gO_gk' }
95+
let(:id) { '3mJE9obZgUc' }
9696

9797
it 'returns valid live streaming details' do
9898
expect(video.actual_start_time).to be_nil
@@ -103,13 +103,13 @@
103103
end
104104

105105
context 'given someone else’s past live video broadcast' do
106-
let(:id) { 'COOM8_tOy6U' }
106+
let(:id) { 'hGil507jyoU' }
107107

108108
it 'returns valid live streaming details' do
109109
expect(video.actual_start_time).to be_a Time
110110
expect(video.actual_end_time).to be_a Time
111-
expect(video.scheduled_start_time).to be_a Time
112-
expect(video.scheduled_end_time).to be_a Time
111+
expect(video.scheduled_start_time).to be_nil
112+
expect(video.scheduled_end_time).to be_nil
113113
expect(video.concurrent_viewers).to be_nil
114114
end
115115
end
@@ -126,13 +126,13 @@
126126
end
127127

128128
context 'given one of my own videos that I want to delete' do
129-
let(:id) { 'eVyohfo-I2Q' }
129+
let(:id) { '505qC3W3DrY' }
130130

131131
it { expect(video.delete).to be true }
132132
end
133133

134134
context 'given one of my own videos that I want to update' do
135-
let(:id) { 'eVyohfo-I2Q' }
135+
let(:id) { '505qC3W3DrY' }
136136
let!(:old_title) { video.title }
137137
let!(:old_privacy_status) { video.privacy_status }
138138
let(:update) { video.update attrs }

spec/requests/as_server_app/channel_spec.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
subject(:channel) { Yt::Channel.new attrs }
66

77
context 'given an existing channel ID' do
8-
let(:attrs) { {id: 'UCAmh1DexLGcMtDlzMCIxo4w'} }
8+
let(:attrs) { {id: 'UCJkWoS4RsldA1coEIot5yDA'} } # MotherGooseClub
99

1010
it 'returns valid snippet data' do
1111
expect(channel.snippet).to be_a Yt::Snippet
@@ -24,12 +24,14 @@
2424
it { expect(channel.related_playlists).to be_a Yt::Collections::Playlists }
2525
it { expect(channel.related_playlists.first).to be_a Yt::Playlist }
2626

27-
specify 'with a public list of subscriptions' do
28-
expect(channel.subscribed_channels.first).to be_a Yt::Channel
29-
end
27+
# NOTE: API doesn't grant access to the subscriptions of other users
28+
# any more.
29+
# specify 'with a public list of subscriptions' do
30+
# expect(channel.subscribed_channels.first).to be_a Yt::Channel
31+
# end
3032

3133
context 'with a hidden list of subscriptions' do
32-
let(:attrs) { {id: 'UCZDZGN_73I019o6UYD2-4bg'} }
34+
let(:attrs) { {id: 'UCBR8-60-B28hp2BmDPdntcQ'} } # YouTube
3335
it { expect{channel.subscribed_channels.size}.to raise_error Yt::Errors::Forbidden }
3436
end
3537
end

spec/requests/as_server_app/comment_thread_spec.rb

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,27 @@
55
describe Yt::CommentThread, :server_app, :vcr do
66
subject(:comment_thread) { Yt::CommentThread.new attrs }
77

8-
context 'given an existing comment thread ID about a channel' do
9-
let(:attrs) { {id: 'UgzzJVW75s5KrSaf0Ah4AaABAg'} }
8+
# Probably, channel-based comment thread has been deprecated.
9+
# Need more investigation but, commented out for now.
10+
# context 'given an existing comment thread ID about a channel' do
11+
# let(:attrs) { {id: 'UgzzJVW75s5KrSaf0Ah4AaABAg'} }
12+
13+
# it { expect(comment_thread.video_id).to be_nil }
14+
# it { expect(comment_thread.total_reply_count).to be_an Integer }
15+
# it { expect(comment_thread.can_reply?).to be true }
16+
# it { expect(comment_thread).to be_public }
17+
18+
# it { expect(comment_thread.top_level_comment).to be_a Yt::Comment }
19+
# it { expect(comment_thread.text_display).not_to be_empty }
20+
# it { expect(comment_thread.author_display_name).not_to be_empty }
21+
# it { expect(comment_thread.updated_at).to be_a Time }
22+
# it { expect(comment_thread.like_count).to be_a Integer }
23+
# end
24+
25+
context 'given an comment thread ID about a video' do
26+
let(:attrs) { {id: 'z134e1gyav3qt3nnr22phjeavv2zdfef0'} }
27+
it { expect(comment_thread.video_id).to be_a String }
1028

11-
it { expect(comment_thread.video_id).to be_nil }
1229
it { expect(comment_thread.total_reply_count).to be_an Integer }
1330
it { expect(comment_thread.can_reply?).to be true }
1431
it { expect(comment_thread).to be_public }
@@ -19,9 +36,4 @@
1936
it { expect(comment_thread.updated_at).to be_a Time }
2037
it { expect(comment_thread.like_count).to be_a Integer }
2138
end
22-
23-
context 'given an comment thread ID about a video' do
24-
let(:attrs) { {id: 'z134e1gyav3qt3nnr22phjeavv2zdfef0'} }
25-
it { expect(comment_thread.video_id).to be_a String }
26-
end
2739
end

spec/requests/as_server_app/comment_threads_spec.rb

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
expect(comment_threads.where(videoId: 'MsplPPW7tFo').size).to be > 0
2121
end
2222

23-
specify 'with a channelId param, returns comment threads for the channel' do
24-
expect(comment_threads.where(channelId: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw').size).to be > 0
25-
end
23+
# Could be undocumented change. Currently not working.
24+
# specify 'with a channelId param, returns comment threads for the channel' do
25+
# expect(comment_threads.where(channelId: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw').size).to be > 0
26+
# end
2627
end
2728

2829
context "with parent association", :ruby2 do
@@ -33,9 +34,18 @@
3334
it { expect(comment_threads.size).to be > 0 }
3435
end
3536

36-
context "parent as channel" do
37-
let(:parent) { Yt::Models::Channel.new id: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw' }
38-
it { expect(comment_threads.size).to be > 0 }
39-
end
37+
# Commented out because possible YouTube behavior change. It does not work
38+
# on 'Try this method' section either, currently. The error looks like:
39+
# {
40+
# "message": "The video identified by the \u003ccode\u003e\u003ca href=\"/youtube/v3/docs/commentThreads/list#videoId\"\u003evideoId\u003c/a\u003e\u003c/code\u003e parameter could not be found.",
41+
# "domain": "youtube.commentThread",
42+
# "reason": "videoNotFound",
43+
# "location": "videoId",
44+
# "locationType": "parameter"
45+
# }
46+
# context "parent as channel" do
47+
# let(:parent) { Yt::Models::Channel.new id: 'UC-lHJZR3Gqxm24_Vd_AJ5Yw' }
48+
# it { expect(comment_threads.size).to be > 0 }
49+
# end
4050
end
4151
end

spec/requests/as_server_app/playlist_item_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
subject(:item) { Yt::PlaylistItem.new id: id }
66

77
context 'given an existing playlist item' do
8-
let(:id) { 'UExiai1JRGU2Zzh2c0FQT0RFci1xRUZjRERvWHhqRzhEVC41MjE1MkI0OTQ2QzJGNzNG' }
8+
let(:id) { 'UExnbkRNdzZ4STVwbE9YYUtzNXpOREIzelJXWUVhOFppLS41NkI0NEY2RDEwNTU3Q0M2' }
99

1010
it 'returns valid snippet data' do
1111
expect(item.snippet).to be_a Yt::Snippet

spec/requests/as_server_app/playlist_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
subject(:playlist) { Yt::Playlist.new attrs }
66

77
context 'given an existing playlist ID' do
8-
let(:attrs) { {id: 'PLpjK416fmKwQ6Ene4GKRLICznoE1QFyt4'} }
8+
let(:attrs) { {id: 'PLgnDMw6xI5plOXaKs5zNDB3zRWYEa8Zi-'} }
99

1010
it 'returns valid snippet data' do
1111
expect(playlist.snippet).to be_a Yt::Snippet

0 commit comments

Comments
 (0)