question

shaqspeare avatar image
shaqspeare asked

Obtaining profile information

[On behalf of: Kush Fanikiso] I am a developer working on integrating Amazon login to a web app. I was following the 'getting started from web' guide but I am not sure where the code in the fourth step (Obtaining profile information) should go. If i put it in a file called / handle_login.php on the server side how do i then obtain the new users name/email to add to my Parse back end and how do i redirect the user back to the page they were on when they clicked the login with amazon button? Thanks in advance Kush
login with amazon
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shaqspeare avatar image
shaqspeare answered
Hi Kush, Inside the handle_login.php file listed in step 4 of the getting started guide, the following sample code obtains the user's name and email address: // exchange the access token for user profile $c = curl_init(' https://api.amazon.com/user/profile'); curl_setopt($c, CURLOPT_HTTPHEADER, array('Authorization: bearer ' . $_REQUEST['access_token'])); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); $r = curl_exec($c); curl_close($c); $d = json_decode($r); This code makes an HTTPS request from your server to the Amazon server that responds to your request for user profiles and returns a JSON formatted response that contains the user's profile. The last line parses the JSON response into a php array stored in the variable named $d. You can use the data in the array $d to store the values on your server in the manner that you prefer. As for your second question about redirecting your users after they have signed in. You can accomplish this by first providing an argument to / handle_login.php in the call to amazon.Login.authorize: amazon.Login.authorize(options, ' https://www.example.com/handle_login.php?next= "); Finally you can add the following to handle_login.php to redirect the user to the new page: Header("Location: https://yoursite.com" . $_REQUEST['next']); Let us know if you still have questions or if this explanation was unclear. Thank you for using Login with Amazon
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shaqspeare avatar image
shaqspeare answered
[On behalf of: Kush Fanikiso] Hi Nicolas, Thanks for the response. It was super clear. I switched my entire backend to Rails so now i'm trying to work with the Ruby implementation but i'm getting the following error while trying to retrieve the access token: {"error"=>"invalid_request", "error_description"=>"Malformed request"} This is my test code: retrieve access_token uri = URI.parse(" https://api.amazon.com/auth/o2/token?grant_type=authorization_code&code={}&client_id={}&client_secret={}&redirect_uri={}") req = Net::HTTP::Post.new(uri.request_uri) req = "application/x-www-form-urlencoded" http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_PEER response = http.request(req) decode = JSON.parse(response.body) puts decode I did not have the redirect uri there before but added it just in case that was the issue. Do you know what the problem might be? Thanks Kush Edited by: Kush Fanikiso on Jul 10, 2013 1:54 PM
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

shaqspeare avatar image
shaqspeare answered
Kush, In your posted code sample, you reassign req to the content type string. I assume this is simply a typo in your post? That error typically indicates a missing or invalid request parameter. Please verify that your request parameters are URL encoded (redirect_uri is the most likely culprit). Also, please check that your redirect URI is in your application's Allowed Return URLs list (see "Add a Website to your Application" under "Register Your Application" in the Getting Started for Web guide). If neither of those suggestions resolve the issue, please send your client ID and redirect URI to lwa-support@amazon.com so that we may continue to investigate.
10 |5000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.