我在尝试用 Retrofit2 发出请求到公司的OAuth后端取回 access token
基本上就是把下面的curl 命令用Retrofit2写一遍
curl -X POST -d "grant_type=password&phone_number=123123123&password=12345" -u "Client_Id:Client_Secret" http://baseURL/o/token
我自己的service接口
public interface LoginService { @POST("/o/token") @FormUrlEncoded Observable getAccessToken(@Field("grant_type") String grantType, @Field("phone_number") String phoneNumber, @Field("password") String password, @Header("Authorization") String authorization); }
下面是实现方法
public void login(final MySubscribe subscriber, String pn, String psw) { mRetrofit = new Retrofit.Builder() .baseUrl(MyFeeds.APP_BASE_URL) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); //encode pn String real_pn = getRealPN(pn); //encode the credential using base64 String credential = MyFeeds.CLIENT_ID+":"+MyFeeds.CLIENT_SECRET; String auth = null; try { auth = "Basic " + getBase64String(credential); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } LoginService service = mRetrofit.create(LoginService.class); Observable observable = service.getAccessToken("password", real_pn, psw, auth); mSubscription = observable .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(subscriber); }
但我只得到 http500 错误.
有人可以帮下吗? 谢谢
==============
request body
500 is an internal server error.
You should check the code behind the scenes.
Refer to the List of HTTP status codes -- 5xx Server Error