2

I'm working on a little Ruby program to collect GitHub URLs from my Pocket account and then star those repos on my GitHub account. I've been able to connect to Pocket's API and get the URLs from my feed, but I'm having trouble with my requests to the "https://api.github.com/user/starred" endpoint.

This is what I'm using

I initially used Paw on my Mac to get the authorization tokens for both Pocket and GitHub - the Pocket API is working perfectly in both Paw and in my Rub, but the GitHub request is only working in Paw.

I've tried both flavors of the Faraday request syntax (you can see them in the code), using Faraday.new and a simple Faraday.get

require 'faraday'
require 'json'

github = Faraday.new(
  url: 'https://api.github.com/user/starred',
  headers: {
    'Accept ': 'application/vnd.github.v3+json',
    'Authorization': 'Bearer ***SUPER_SECRET_SAUCE***',
    'User-Agent': 'jriggles'
  }
)

pocket = Faraday.get(
  'https://getpocket.com/v3/get?consumer_key=***MY_POCKET_KEY**&access_token=***MY_POCKET_TOKEN***&contentType=article&detailType=simple&sort=newest&domain=github.com&count=100',
  'Content-Type': 'application/json; charset=UTF8',
  'X-Accept': 'application/json',
  'User-Agent': 'jriggles'
)

if pocket.status == 200
  parsed_response = JSON.parse(pocket.body)
  parsed_response['list'].each do |_key, val|
    owner = val['given_url'].split('/')[3]
    repo = val['given_url'].split('/')[4]
    puts github.get("/#{owner}/#{repo}").status
  end
else puts "Connection to Pocket failed - Code #{pocket.status}"
end

This is the line that's returning the HTTP status codes:

puts github.get("#{owner}/#{repo}").status

I've tried a few permutations, and I've only been getting [401], [403], and in this current iteration [400]. I'm not sure if it's an issue with my GitHub authorization or with my Faraday syntax, or both.

JRiggles
  • 4,847
  • 1
  • 12
  • 27

2 Answers2

0

Seems like using Faraday to make pocket requests is working for you, which suggests that you are probably using it correctly.

Looking at the GitHub documentation https://developer.github.com/v3/#authentication I would suggest replacing 'Bearer' with 'token' and see if that helps.

Yoav Epstein
  • 849
  • 9
  • 7
  • I gave it a shot! I'm afraid I'm still getting code 400. – JRiggles Aug 05 '19 at 10:58
  • _Additional Information_ - Using either the token type "Bearer" or "Token" in Paw gives me code 200...for some reason it's working there, and it doesn't seem to matter which I use. – JRiggles Aug 05 '19 at 11:06
0

I figured it out! I am a potato.

There is a typo in the header parameters for my GitHub API request:

'Accept ': 'application/vnd.github.v3+json'

should be

'Accept': 'application/vnd.github.v3+json',

I had a space after 'Accept'!

JRiggles
  • 4,847
  • 1
  • 12
  • 27