question

newuser-5ae29fa7-b28d-459b-900d-5f74defdcdf3 avatar image

Unable to get refresh token and access token for amazon drs

I am trying to build a device that makes automatic order in Amazon when the quantity of the product in the device runs low using Amazon DRS. I had obtained the authorization code, but I am stucked in getting refresh token and access token. I am using this AmazonDRS library for my project. When I run this code the esp8266 getting reset. Please help me to get out of this.

 
                



#ifdef ESP8266

#include <ESP8266WiFi.h>

#include <WiFiClientSecure.h>

#else

#include <WiFi101.h>

#endif


#include "AmazonDRS.h"


AmazonDRS DRS = AmazonDRS();


//WiFi creds ----------------------------------------------------------------------------------

char ssid[] = ""; // your network SSID (name)

char pass[] = ""; // your network password (use for WPA, or use as key for WEP)

//------------------------------------------------------------------------------------------------------


//Extract the code from the response during the LWA process, example response below

//https://app.getpostman.com/oauth2/callback?code=ANQEgiAOjkQWjKvhNWIN&scope=dash%3Areplenish

//------------------------------------------------^^^^^^^^^^^^^^^^^^^^-----------------------

#define auth_code "ANqbDAujrtMgtplRkYgx" //ex: ANQEgiAOjkQWjKvhNWIN

int status = WL_IDLE_STATUS;


void setup() {

Serial.begin(115200);


while (!Serial) {

; // wait for serial port to connect. Needed for native USB port only

}


#ifdef ESP8266

WiFiClientSecure client;

#else

WiFiSSLClient client;

#endif


//Start up DRS

DRS.begin(&client);


//connect to WiFi

Serial.println("Initializing DRS... connecting to WiFi");

while (status != WL_CONNECTED) {

// Connect to WPA/WPA2 network. Change this line if using open or WEP network:

status = WiFi.begin(ssid, pass);

Serial.println(".");

delay(3000);

status = WiFi.status();

}


Serial.print("Connected to ");

Serial.println(WiFi.SSID());


DRS.setAuthCode(auth_code);

DRS.requestBearerAndRefreshTokens(); //only call this on initial setup - once user has authorized

//LWA for the device just use the refresh_token

delay(3000); //wait a moment for the authorization code grant to complete the exchange


DRS.retrieveSubscriptionInfo(); //test if the tokens are valid by checking slot statuses


Serial.println("Copy everything below...");

Serial.println(DRS.getRefreshToken()); //Print the refresh_token to the screen


/*copy this to AmazonTokens.h and set refresh_token equal to what's printed to the terminal

Should look like this (~460 characters)...

refresh token = "Atzr|IwEBILdfysz66E9sRHsUobHgfh1X_h-esnBfcCdYjdcCfhGRkZqXujzXSN_3a8yqj5btX1B6NgbrmEX6wax_wmJ7Sgaaa39GbR-6hjDt_tHpKsFXPGwnIhy-14CWuE4oeYDWG4pCvQ4JEMKk2DiAsuwlUtoOVwaEOif1gWErh5rswCJ8mRhaXQ7SJhZB0CWYHm_ZA_PY8xTTVTUcZFqP7iz8kBw5QGDePyOb8NvJvSuBYYkwRQTj-qrytfdcHwMWOJc5QdoyPFpmchSfsMUpMqjDmwVPBfFzb0xZWYKxdUerSeKV1VVlS4Bwl2j-4gHnjHGohUsxau4Bn9SfG9McP-7RqD9Vmk3g--rsfACQ-uVLCJSJ29sBEMNkA5sxh9E9fpTwEw166WY-xBfYa_XB9aAU3n6Fn2yFM0I7ZpPIY1fy0gkdYkFOFD0uAOdoTFDxGXwbBWE";


From now on you can use the refresh_token to authorize API calls for as long as you keep your app

authrozied for LWA in "Your Account > Manage Login with Amazon"*/


}


void loop() {


}

login with amazondash replenishment service
10 |5000

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

1 Answer

Levon@Amazon avatar image
Levon@Amazon answered

Hi there,

Thanks for posting! Please do not share your refresh token on public forums. As for the issue, there can be multiple things.

Our guess is the code verifier is wrong or codeChallengeMethod is wrong.
More info about it here:
https://developer.amazon.com/docs/dash/lwa-mobile-sdk.html

If the data you are passing is correct, you should be able to build a cURL request as stated in docs.We have public sample apps that you can look at on GitHub: iOS, AndroidYour issue is probably in the following methods:

IdentityHandlerhttps://github.com/amzn/drs-sample-ios-app/blob/master/DrsSampleIOSApp/Utilities/IdentityHandler.m#L124

TokenGeneratorhttps://github.com/amzn/drs-sample-ios-app/blob/master/DrsSampleIOSApp/Utilities/TokenGenerator.m#L163

10 |5000

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