diff --git a/lib/threads/api/oauth2/client.rb b/lib/threads/api/oauth2/client.rb index 349f369..c934f0a 100644 --- a/lib/threads/api/oauth2/client.rb +++ b/lib/threads/api/oauth2/client.rb @@ -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:) @@ -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) diff --git a/spec/threads/api/oauth2/client_spec.rb b/spec/threads/api/oauth2/client_spec.rb index 2b40748..854f29d 100644 --- a/spec/threads/api/oauth2/client_spec.rb +++ b/spec/threads/api/oauth2/client_spec.rb @@ -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