Dragma

POST /api/v3/users/_token/ in Android

Recommended Posts

Starting over in Android I am once again having some issues:  I keep on getting a 403 error with this call,  here is the code I am using from Android Studio:

url = new URL("https://rebrickable.com/api/v3/users/_token/");
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "key {myKey}");
conn.setRequestMethod("POST");

// Send the post body

if (prop.PostData != null) {
  String postData = prop.PostData.toString();
  //example postData:
  //"{password=******, username=********}"
  OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
  writer.write(postData);
  writer.flush();
}

int statusCode = conn.getResponseCode();
if (statusCode ==  200) {
  InputStream inputStream = new BufferedInputStream(conn.getInputStream());
  response = convertStreamToString(inputStream);
}

Any ideas?

Share this post


Link to post
Share on other sites

Hi Dragma,

1 hour ago, Dragma said:

conn.setRequestProperty("Authorization", "key {myKey}");

I guess that {mykey} will really be replaced with your API-key here and you've used the {myKey}-notation just for clearing out your real key. If not, there may be a problem here.

1 hour ago, Dragma said:

String postData = prop.PostData.toString(); //example postData: //"{password=******, username=********}" OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

At the end of the day when going over the wire, post-data key-valuepairs have to be separated by a "&" and not ",". Otherwise the comma will be part of the password and this breaks your authorization (which explains the 403 - Not authorized)

Maybe it helps

Dietmar

Share this post


Link to post
Share on other sites
4 hours ago, dkurok said:

Hi Dragma,

I guess that {mykey} will really be replaced with your API-key here and you've used the {myKey}-notation just for clearing out your real key. If not, there may be a problem here.

At the end of the day when going over the wire, post-data key-valuepairs have to be separated by a "&" and not ",". Otherwise the comma will be part of the password and this breaks your authorization (which explains the 403 - Not authorized)

Maybe it helps

Dietmar

Yes the myKey and ****** are to hide my info.

And I had tried swapping the "," with "&", And the curly braces with nothing.

 

Though with a clearer head in the morn I may have a few ideas.

Edited by Dragma

Share this post


Link to post
Share on other sites

I think I may have it, the CURL I get from the test page is:

curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: application/json' --header 'Authorization: key {myKey}' -d 'username=********&password=*******' 'https://rebrickable.com/api/v3/users/_token/'

it looks like I just need to make sure the username is first,  and separated by &, and maybe single quotes surrounding the login and password.

I'll test tonight and post my responce.  It could help someone else.

Share this post


Link to post
Share on other sites

Finally got it,  sheesh I did not have this much trouble doing it in c#.  Anyways if anyone else has the problem:

String postData =  "username=" + URLEncoder.encode ("*******@***.***") + "&password=" + URLEncoder.encode ("********");
URL url = new URL("https://rebrickable.com/api/v3/users/_token/?key={MyKey}");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestMethod("POST");

// Write body
OutputStream output = conn.getOutputStream();
output.write(postData.getBytes());
int statusCode = conn.getResponseCode();
if (statusCode == 200) {
    InputStream inputStream = new BufferedInputStream(conn.getInputStream());
    response = convertStreamToString(inputStream);
}
output.close();

it seems the key for this method is to build a flat string with the properties and their encoded values instead of doing from a Key / encoded Value pair.  In this case the postData looks like:

username=******%40*****.***&password=*******

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now