Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/muffin_man/credential/v20231130.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module MuffinMan::Credential
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is usually named as mentioned in the SP-API documentation. So this would be MuffinMan::ApplicationManagement

class V20231130
NEW_APP_CREDENTIAL_URL = "https://sellingpartnerapi-na.amazon.com/applications/2023-11-30/clientSecret".freeze

def self.create_new_credentials(access_token)
response = Typhoeus.post(
NEW_APP_CREDENTIAL_URL,
headers: {
"x-amz-access-token" => "#{access_token}"
}
)
if response.code != 204
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just returning the response would be better as users of the client can decide further action.

error_body = JSON.parse(response.body)
error = "#{error_body['error']}: #{error_body['error_description']} "
raise MuffinMan::Error, error
end
response.code
end
end
end
22 changes: 22 additions & 0 deletions lib/muffin_man/lwa/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,27 @@ def self.get_refresh_token(client_id, client_secret, auth_code)
end
JSON.parse(response.body)["refresh_token"]
end

def self.get_client_credential_refresh_token(client_id, client_secret)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nitinbhakar @sanjjaymahalingam I am not sure about this can you check once, in muffin man sp_api_client.rb we have method called retrieve_lwa_access_token will that be useful to generate the refresh_token? and then use for client creation?

Copy link
Contributor Author

@nitinbhakar nitinbhakar Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rohit-lingayat-sd Yes that can be useful but thats a private method so can't call it directly.
So should i use send() to call private method?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm no, then we can go with your approach then

body = {
grant_type: "client_credentials",
client_id: client_id,
client_secret: client_secret
}

response = Typhoeus.post(
ACCESS_TOKEN_URL,
body: URI.encode_www_form(body),
headers: {
"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
}
)
if response.code != 200
error_body = JSON.parse(response.body)
error = "#{error_body["error"]}: #{error_body["error_description"]}"
raise MuffinMan::Error, error
end
JSON.parse(response.body)["refresh_token"]
end
end
end
Loading