我需要在IOS中使用ssl apis进行oauth请求。我尝试过 afnetworking 框架,但它不起作用。
我在android中使用这个库做了同样的事情 android-async-http-master
我需要在iphone中做同样的事情,但我从任何地方都没有得到适当的想法。 如果有人做了同样的事情,那么请帮我解决这个问题。谢谢
这是我的代码,
APIClient.class
public class APIClient {
private static String Base_URL = "http://**************:8080";
private static AsyncHttpClient client = new AsyncHttpClient(false, 80, 443);
@SuppressWarnings("deprecation")
public static void prepareAllRequest() {
// clearBasicAuth deprecated, might will be used with
// clearCredentialsProvider instead
client.clearBasicAuth();
}
public static void prepareAuthRequest(String access_token) {
prepareAllRequest();
client.addHeader("Authorization", "Bearer " + access_token);
}
public static void get(String url, String access_token,
AsyncHttpResponseHandler responseHandler) {
prepareAuthRequest(access_token);
client.get(getAbsoluteUrl(url), responseHandler);
}
public static void post(String url, String access_token,
RequestParams params, AsyncHttpResponseHandler responseHandler) {
prepareAuthRequest(access_token);
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static void post_auth(String url, RequestParams params,
AsyncHttpResponseHandler responseHandler) {
client.setBasicAuth("**************", "**************");
client.post(getAbsoluteUrl(url), params, responseHandler);
}
public static void register(RequestParams params,
AsyncHttpResponseHandler responseHandler) {
prepareAllRequest();
client.post(getAbsoluteUrl("/signup"), params, responseHandler);
}
public static void delete(String url, String access_token,
RequestParams params, AsyncHttpResponseHandler responseHandler) {
prepareAuthRequest(access_token);
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return Base_URL + relativeUrl;
}
}
MainActivity.class
public class MainActivity extends Activity {
private JsonHttpResponseHandler GGHandler;
public String access_token = "";
public String refresh_token = "";
private String user_code = "**************";
private String user_email = "**************";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
authentication();
}
private void authentication() {
final RequestParams param2 = new RequestParams();
param2.put("username", "************");
param2.put("password", "************");
param2.put("client_id", "************");
param2.put("client_secret", "************");
param2.put("scope", "read");
param2.put("grant_type", "password");
GGHandler = new JsonHttpResponseHandler() {
public void onFailure(int i, Header aheader[], String s,
Throwable throwable) {
Log.e("CANCER", (new StringBuilder()).append(i).append(" - ")
.append(s).toString());
Log.e("CANCER", throwable.getMessage());
APIClient.post_auth("/oauth/token", param2, GGHandler);
}
public void onFailure(int i, Header aheader[], Throwable throwable,
JSONObject jsonobject) {
StringBuilder stringbuilder = (new StringBuilder()).append(i)
.append(" - ");
if (jsonobject == null) {
// aheader = "";
} else {
Log.e("test", "data from i :" + i);
// aheader = jsonobject.toString();
}
Log.e("test", "data from failer :" + jsonobject.toString());
Log.e("CANCER2", stringbuilder.append(aheader).toString());
Log.e("CANCER2", throwable.getMessage());
if (i == 0) {
APIClient.post_auth("/oauth/token", param2, GGHandler);
}
if (i == 400) {
Toast.makeText(getApplicationContext(),
"data is not send ", 1).show();
}
}
public void onSuccess(int i, Header aheader[], JSONObject jsonobject) {
try {
access_token = jsonobject.getString("access_token");
refresh_token = jsonobject.getString("refresh_token");
Log.e("test",
"data from jsonobject :"
+ jsonobject.getString("access_token"));
Log.e("test",
"data from jsonobject :"
+ jsonobject.getString("refresh_token"));
GetandDisplayListofscene();
Log.e("Status code",
(new StringBuilder()).append("Status code is: ")
.append(i).toString());
// Misplaced declaration of an exception variable
} catch (Exception e) {
e.printStackTrace();
}
}
};
APIClient.post_auth("/oauth/token", param2, GGHandler);
}
private void signup() {
RequestParams requestparams;
requestparams = new RequestParams();
requestparams.put("username", "************");
requestparams.put("password", "************");
requestparams.put("email", "************");
Log.e("Register isSocial",
(new StringBuilder()).append("").append("false").toString());
Log.e("Register username", user_email);
APIClient.register(requestparams, new TextHttpResponseHandler() {
public void onFailure(int i, Header aheader[], String s1,
Throwable throwable) {
Log.e("Post",
(new StringBuilder())
.append("Failed with status Code ").append(i)
.toString());
Log.e("Post",
(new StringBuilder()).append("Failed with response ")
.append(throwable).toString());
}
public void onSuccess(int i, Header aheader[], String s1) {
Log.e("test", "data in sign up :" + s1);
authentication();
}
});
}
}