简体   繁体   中英

Ruby on Rails api with android

I am having trouble getting a 401 unauthorized response from the server,I am doing a "GET" call from a ruby on rails api the oauth token is in the header like this.

  urlConnection = (HttpURLConnection) url.openConnection();
            OAuth =   "bearer " + Base64.encodeToString(userpass.getBytes(), Base64.NO_WRAP);
            Log.e("auth", oAuth_token);
            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("Authorization", OAuth);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.connect();

Userpass is the actual token.

This works in postman as well as on iOS but android is getting a 401

The Bearer Token Usage specification ( RFC 6750 ) is a bit confusing. You don't have to base64-encode the access token before use. To be concrete, your code should be simply like this:

OAuth = "Bearer " + userpass;

See this discussion where the questioner confessed he had had the same confusion as yours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM