Skip to content

Commit 9bd97ca

Browse files
authored
Merge pull request #308 from keynslug/master
Supply access token through Authorization header
2 parents 02ab846 + 635b143 commit 9bd97ca

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

build/gist

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ module Gist
14161416
def multi_gist(files, options={})
14171417
if options[:anonymous]
14181418
raise 'Anonymous gists are no longer supported. Please log in with `gist --login`. ' \
1419-
'(Github now requires credentials to gist https://bit.ly/2GBBxKw)'
1419+
'(GitHub now requires credentials to gist https://bit.ly/2GBBxKw)'
14201420
else
14211421
access_token = (options[:access_token] || auth_token())
14221422
end
@@ -1442,9 +1442,9 @@ module Gist
14421442

14431443
url = "#{base_path}/gists"
14441444
url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
1445-
url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''
14461445

14471446
request = Net::HTTP::Post.new(url)
1447+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
14481448
request.body = JSON.dump(json)
14491449
request.content_type = 'application/json'
14501450

@@ -1480,9 +1480,10 @@ module Gist
14801480
if user == ""
14811481
access_token = auth_token()
14821482
if access_token.to_s != ''
1483-
url << "/gists?access_token=" << CGI.escape(access_token)
1483+
url << "/gists"
14841484

14851485
request = Net::HTTP::Get.new(url)
1486+
request['Authorization'] = "token #{access_token}"
14861487
response = http(api_url, request)
14871488

14881489
pretty_gist(response)
@@ -1507,8 +1508,8 @@ module Gist
15071508
if user == ""
15081509
access_token = auth_token()
15091510
if access_token.to_s != ''
1510-
url << "/gists?per_page=100&access_token=" << CGI.escape(access_token)
1511-
get_gist_pages(url)
1511+
url << "/gists?per_page=100"
1512+
get_gist_pages(url, access_token)
15121513
else
15131514
raise Error, "Not authenticated. Use 'gist --login' to login or 'gist -l username' to view public gists."
15141515
end
@@ -1524,11 +1525,9 @@ module Gist
15241525
url = "#{base_path}/gists/#{id}"
15251526

15261527
access_token = auth_token()
1527-
if access_token.to_s != ''
1528-
url << "?access_token=" << CGI.escape(access_token)
1529-
end
15301528

15311529
request = Net::HTTP::Get.new(url)
1530+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
15321531
response = http(api_url, request)
15331532

15341533
if response.code == '200'
@@ -1554,9 +1553,8 @@ module Gist
15541553

15551554
access_token = auth_token()
15561555
if access_token.to_s != ''
1557-
url << "?access_token=" << CGI.escape(access_token)
1558-
15591556
request = Net::HTTP::Delete.new(url)
1557+
request["Authorization"] = "token #{access_token}"
15601558
response = http(api_url, request)
15611559
else
15621560
raise Error, "Not authenticated. Use 'gist --login' to login."
@@ -1569,17 +1567,18 @@ module Gist
15691567
end
15701568
end
15711569

1572-
def get_gist_pages(url)
1570+
def get_gist_pages(url, access_token = "")
15731571

15741572
request = Net::HTTP::Get.new(url)
1573+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
15751574
response = http(api_url, request)
15761575
pretty_gist(response)
15771576

15781577
link_header = response.header['link']
15791578

15801579
if link_header
15811580
links = Hash[ link_header.gsub(/(<|>|")/, "").split(',').map { |link| link.split('; rel=') } ].invert
1582-
get_gist_pages(links['next']) if links['next']
1581+
get_gist_pages(links['next'], access_token) if links['next']
15831582
end
15841583

15851584
end
@@ -1652,7 +1651,7 @@ module Gist
16521651
# @option credentials [String] :password
16531652
# @see http://developer.github.com/v3/oauth/
16541653
def login!(credentials={})
1655-
puts "Obtaining OAuth2 access_token from github."
1654+
puts "Obtaining OAuth2 access_token from GitHub."
16561655
loop do
16571656
print "GitHub username: "
16581657
username = credentials[:username] || $stdin.gets.strip
@@ -1906,7 +1905,7 @@ filenames can be overridden by repeating the "-f" flag. The most useful reason
19061905
to do this is to change the syntax highlighting.
19071906
19081907
All gists must to be associated with a GitHub account, so you will need to login with
1909-
`gist --login` to obtain an Oauth2 access token. This is stored and used by gist in the future.
1908+
`gist --login` to obtain an OAuth2 access token. This is stored and used by gist in the future.
19101909
19111910
Private gists do not have guessable URLs and can be created with "-p", you can
19121911
also set the description at the top of the gist by passing "-d".
@@ -2023,7 +2022,7 @@ end.parse!
20232022
begin
20242023
if Gist.auth_token.nil?
20252024
puts 'Please log in with `gist --login`. ' \
2026-
'(Github now requires credentials to gist https://bit.ly/2GBBxKw)'
2025+
'(GitHub now requires credentials to gist https://bit.ly/2GBBxKw)'
20272026
exit(1)
20282027
end
20292028

lib/gist.rb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ def multi_gist(files, options={})
136136

137137
url = "#{base_path}/gists"
138138
url << "/" << CGI.escape(existing_gist) if existing_gist.to_s != ''
139-
url << "?access_token=" << CGI.escape(access_token) if access_token.to_s != ''
140139

141140
request = Net::HTTP::Post.new(url)
141+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
142142
request.body = JSON.dump(json)
143143
request.content_type = 'application/json'
144144

@@ -174,9 +174,10 @@ def list_gists(user = "")
174174
if user == ""
175175
access_token = auth_token()
176176
if access_token.to_s != ''
177-
url << "/gists?access_token=" << CGI.escape(access_token)
177+
url << "/gists"
178178

179179
request = Net::HTTP::Get.new(url)
180+
request['Authorization'] = "token #{access_token}"
180181
response = http(api_url, request)
181182

182183
pretty_gist(response)
@@ -201,8 +202,8 @@ def list_all_gists(user = "")
201202
if user == ""
202203
access_token = auth_token()
203204
if access_token.to_s != ''
204-
url << "/gists?per_page=100&access_token=" << CGI.escape(access_token)
205-
get_gist_pages(url)
205+
url << "/gists?per_page=100"
206+
get_gist_pages(url, access_token)
206207
else
207208
raise Error, "Not authenticated. Use 'gist --login' to login or 'gist -l username' to view public gists."
208209
end
@@ -218,11 +219,9 @@ def read_gist(id, file_name=nil)
218219
url = "#{base_path}/gists/#{id}"
219220

220221
access_token = auth_token()
221-
if access_token.to_s != ''
222-
url << "?access_token=" << CGI.escape(access_token)
223-
end
224222

225223
request = Net::HTTP::Get.new(url)
224+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
226225
response = http(api_url, request)
227226

228227
if response.code == '200'
@@ -248,9 +247,8 @@ def delete_gist(id)
248247

249248
access_token = auth_token()
250249
if access_token.to_s != ''
251-
url << "?access_token=" << CGI.escape(access_token)
252-
253250
request = Net::HTTP::Delete.new(url)
251+
request["Authorization"] = "token #{access_token}"
254252
response = http(api_url, request)
255253
else
256254
raise Error, "Not authenticated. Use 'gist --login' to login."
@@ -263,17 +261,18 @@ def delete_gist(id)
263261
end
264262
end
265263

266-
def get_gist_pages(url)
264+
def get_gist_pages(url, access_token = "")
267265

268266
request = Net::HTTP::Get.new(url)
267+
request['Authorization'] = "token #{access_token}" if access_token.to_s != ''
269268
response = http(api_url, request)
270269
pretty_gist(response)
271270

272271
link_header = response.header['link']
273272

274273
if link_header
275274
links = Hash[ link_header.gsub(/(<|>|")/, "").split(',').map { |link| link.split('; rel=') } ].invert
276-
get_gist_pages(links['next']) if links['next']
275+
get_gist_pages(links['next'], access_token) if links['next']
277276
end
278277

279278
end

0 commit comments

Comments
 (0)