title: Reddit Api unsupported_grant_type error with Retrofit (java)
body: <p>I am using the "Token Retrieval (code flow)" as outlined <a href="https://github.com/reddit/reddit/wiki/OAuth2" rel="nofollow noreferrer">here</a> to retrieve an access token via OAuth2 for the Reddit Api. I have managed to get the "code" String, but I get an unsupported_grant_type error when attempting to retrieve the access token. I would like to use Retrofit2 to accomplish this.</p>
  
  <pre><code>private void getAccessToken(String code) {
  
      if (mRetrofit == null) {
          mRetrofit = new Retrofit.Builder()
                  .baseUrl(RedditConstants.REDDIT_BASE_URL)
                  .addConverterFactory(GsonConverterFactory.create())
                  .build();
      }
      Api api = mRetrofit.create(Api.class);
  
      String authString = RedditConstants.REDDIT_CLIENT_ID + ":";
      String encodedAuthString = Base64.encodeToString(authString.getBytes(), Base64.NO_WRAP);
  
      Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
      // headers.put("Content-Type", "application/x-www-form-urlencoded");
      // headers.put("Accept", "application/json");
      headers.put("Authorization", "Basic " + encodedAuthString);
      headers.put("grant_type" , "authorization_code");
      headers.put("code", code);
      headers.put("redirect_uri", RedditConstants.REDDIT_REDIRECT_URL);
      headers.put("User-Agent", RedditConstants.REDDIT_USER_AGENT);
  
      Call&lt;RedditAccessToken&gt; call = api.login(headers, MediaType.parse("application/x-www-form-urlencoded"));
      call.enqueue(new Callback&lt;RedditAccessToken&gt;() {
          @Override
          public void onResponse(Call&lt;RedditAccessToken&gt; call, Response&lt;RedditAccessToken&gt; response) {
                  Log.d("Findme", "body: " + response.body().toString());
                  Log.d("Findme", "response: " + response.toString());
          }
  
          @Override
          public void onFailure(Call&lt;RedditAccessToken&gt; call, Throwable t) {
              Log.e("Fineme", "onFailure: " + t.getMessage());
          }
      });
  
  }
  </code></pre>
  
  <p>Log Results:</p>
  
  <pre><code>D/Findme: body: RedditAccessToken{scope='null', token_type='null', 
  error='unsupported_grant_type', expires_in='0', access_token='null', 
  refresh_token='null'}
  D/Findme: response: Response{protocol=h2, code=200, message=, 
  url=https://www.reddit.com/api/v1/access_token/}
  </code></pre>
  
  <p>Retrofit Interface: </p>
  
  <pre><code>public interface Api {
  
      @POST("access_token/")
      Call&lt;RedditAccessToken&gt; login (
          @HeaderMap Map&lt;String, String&gt; headers,
          @Header("Content-Type") MediaType contentType
      );
  }
  </code></pre>
  
  <p>Retrofit Data Model:</p>
  
  <pre><code>public class RedditAccessToken {
  
      @SerializedName("scope")
      @Expose
      private String scope;
  
      @SerializedName("token_type")
      @Expose
      private String token_type;
  
      @SerializedName("expires_in")
      @Expose
      private long expires_in;
  
      @SerializedName("access_token")
      @Expose
      private String access_token;
  
      @SerializedName("refresh_token")
      @Expose
      private String refresh_token;
  
      @SerializedName("error")
      @Expose
      private String error;
  
      public String getScope() {
          return scope;
      }
  
      public String getRefresh_token() {
          return refresh_token;
      }
  
      public void setRefresh_token(String refresh_token) {
          this.refresh_token = refresh_token;
      }
  
      public void setScope(String scope) {
          this.scope = scope;
      }
  
      public String getToken_type() {
          return token_type;
      }
  
      public void setToken_type(String token_type) {
          this.token_type = token_type;
      }
  
      public long getExpires_in() {
          return expires_in;
      }
  
      public void setExpires_in(long expires_in) {
          this.expires_in = expires_in;
      }
  
      public String getAccess_token() {
          return access_token;
      }
  
      public void setAccess_token(String access_token) {
          this.access_token = access_token;
      }
  
      @Override
      public String toString() {
          return "RedditAccessToken{" +
                  "scope='" + scope + '\'' +
                  ", token_type='" + token_type + '\'' +
                  ", error='" + error + '\'' +
                  ", expires_in='" + expires_in + '\'' +
                  ", access_token='" + access_token + '\'' +
                  ", refresh_token='" + refresh_token + '\'' +
                  '}';
      }
  
      public String getError() {
          return error;
      }
  
      public void setError(String error) {
          this.error = error;
      }
  }
  </code></pre>