所以我尝试使用改造来从我们的OAuth安全后端获取访问令牌。
基本上我需要将以下cURL命令转换为改装:
curl -X POST -d "grant_type=password&phone_number=123123123&password=12345" -u"Client_Id:Client_Secret" http://baseURL/o/token
这是我的服务
public interface LoginService {
@POST("/o/token")
@FormUrlEncoded
Observable<LoginEntity> getAccessToken(@Field("grant_type") String grantType,
@Field("phone_number") String phoneNumber,
@Field("password") String password,
@Header("Authorization") String authorization);
}
这是我的服务生成器代码:
public void login(final MySubscribe<LoginEntity> 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<LoginEntity> observable = service.getAccessToken("password", real_pn, psw, auth);
mSubscription = observable.subscribeOn(Schedulers.newThread())
.doOnSubscribe(new Action0() {
@Override
public void call() {
subscriber.onBegin();
}
})
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
但是我一直收到http500错误。
我已阅读以下帖子,但它没有帮助 CURL -u request with Retrofit
有人可以帮忙吗?感谢