Skip to content

Commit

Permalink
Return error info for access token requests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcelis committed Jun 19, 2024
1 parent a4ad4cd commit 0fb71f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/threads/api/oauth2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Threads
module API
module OAuth2
class Client
ShortLivedResponse = Struct.new(:access_token, :user_id)
ShortLivedResponse = Struct.new(:access_token, :user_id, :error_type, :error_message, :code)
LongLivedResponse = Struct.new(:access_token, :token_type, :expires_in)

def initialize(client_id:, client_secret:)
Expand All @@ -19,7 +19,7 @@ def access_token(code:, redirect_uri:)
redirect_uri: redirect_uri
})

ShortLivedResponse.new(*response.body.values_at("access_token", "user_id"))
ShortLivedResponse.new(*response.body.values_at("access_token", "user_id", "error_type", "error_message", "code"))
end

def exchange_access_token(access_token)
Expand Down
13 changes: 13 additions & 0 deletions spec/threads/api/oauth2/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@
expect(response.access_token).to eq("ACCESS_TOKEN")
expect(response.user_id).to eq(1234567890)
end

context "when an error occurs" do
let!(:request) do
stub_request(:post, "https://graph.threads.net/oauth/access_token")
.to_return(body: {error_type: "invalid_request", error_message: "Invalid redirect URI", code: 400}.to_json, headers: {"Content-Type" => "application/json"})
end

it "returns an error response" do
expect(response.error_type).to eq("invalid_request")
expect(response.error_message).to eq("Invalid redirect URI")
expect(response.code).to eq(400)
end
end
end

describe "#exchange_access_token" do
Expand Down

0 comments on commit 0fb71f2

Please sign in to comment.