private void login() {
androidID = Secure.getString(MainActivity.this.getContentResolver(), Secure.ANDROID_ID);
String uP = androidID.concat(":ClientTrustedSecret");
byte[] authByteAry = null;
try {
authByteAry = uP.getBytes("UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
String base64 = Base64.encodeToString(authByteAry, Base64.DEFAULT).trim();
client.addHeader("Authorization", "Basic ".concat(base64));
// Following format is required to post to OAuth
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("grant_type", "password");
jsonObject.put("username", "abc");
jsonObject.put("password", "abc");
} catch (JSONException e1) {
e1.printStackTrace();
}
String contentType = "application/json; charset=UTF-8";
StringEntity data = null;
try {
// Send the json to the server
data = new StringEntity(jsonObject.toString());
client.post(MainActivity.this, baseURL.concat("/tokens"), data, contentType, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
oauthAccessTokenString = jsonObject.get("access_token").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t, String err) {
System.out.println("login failed");
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
以上是我的登录方式。当我拨打另一个网络服务电话时,我得到了未经授权的信息。解锁方法需要以下标头。
private void unlock()
{
AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
asyncHttpClient.addHeader("Locale", "en_US");
asyncHttpClient.addHeader("X-Originator-Type", "app");
asyncHttpClient.addHeader("Content-Type", "application/json");
// asyncHttpClient.addHeader("Connection", "Keep-Alive");
// asyncHttpClient.addHeader("X-Device-Id", androidID);
// asyncHttpClient.addHeader("X-via", deviceId);
// asyncHttpClient.addHeader("ClientID", "abc@abc.com");
asyncHttpClient.addHeader("Authorization", "Bearer ".concat(oauthAccessTokenString));
asyncHttpClient.get("host/users?loginName=abc@abc.com", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
System.out.println(response);
}
@Override
public void onFailure(Throwable t, String err) {
System.out.println("Unlock server call failed");
Log.d("printing error: ", err);
}
});
}
上面的代码抛出 401 未经授权的异常。我拥有的文档中没有引用来回调 url。我提供了 oauth 访问令牌就好了,但是为什么我仍然得到 401?秘密与第二次通话有什么关系吗?有人告诉我,我需要以这种方式设置标题。我还被告知“对于 https,客户端需要能够处理证书的验证。有谁知道如何解决它?