Marcus Gabilheri
Marcus Gabilheri

Reputation: 1289

Retrofit -u username:password fields and scopes array

I am using Retrofit to build a Android Github app but I am having troubles retrieving the token from https://api.github.com/authorizations

I know how to do the request with curl with the command:

curl -X POST -H "Accept: Application/json" -u "username:password" -d '{"scopes": ["user","repo","gist","notifications","repo","repo:status"],"note":"GithubViewer Android App","client_id":"xxx","client_secret":"yyyy"}' "https://api.github.com/authorizations"

I am having troubles knowing how to set up the Scopes array and the -u authentication field. I've tried in the command line using "Authentication: Basic username:password" but that does not work either :(

Here is a sample of what I am trying to do:

RequestInterceptor interceptor = new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
                   request.addHeader("Authentication", "Basic " + username + ":" + password);
           }
        };

 RestAdapter restAdapter = new RestAdapter.Builder()
         .setEndpoint(GithubClient.API_URL)
          .setRequestInterceptor(interceptor)
          .build();

Any suggestions?

Thank you!

Upvotes: 0

Views: 1452

Answers (1)

guido
guido

Reputation: 184

I haven't used Retrofit, but I am spotting something that might cause your problems. You are supplying the username:password as plain text in the Authentication header, it should however be encoded using Base64 encoding.

String str = "username:password";
String base64EncodedUsernamePassword;

try {
    base64EncodedUsernamePassword = Base64.encode(str.getBytes("UTF-8"), Base64.NO_WRAP);
} catch (UnsupportedEncodingException e) {
    // Weird, no UTF-8 encoding found?
}

Upvotes: 1

Related Questions