我目前有一个基于PHP的PHP应用程序(使用Symfony2
和FOSUserbundle
FOSOAuthServerBundle
构建。我目前正在尝试让我的服务器对尝试从其移动设备(Android)登录的用户进行身份验证,然后发送回access token
和refresh token
。
理想情况下,结果是:用户将其规范的用户名和密码输入客户端(移动应用程序),用户通过身份验证,服务器使用访问令牌和刷新令牌进行响应。用户可以访问仅与其相关的信息。 (我不确定隐含授权或密码流在这里是否更有意义)
到目前为止,我已经设法从服务器收到响应,但我总是得到一个JSON响应说明:无效请求。更确切地说:
Error: org.json.JSONException: Value {"error_description":"Invalid grant_type parameter or parameter missing","error":"invalid_request"} of type org.json.JSONObject cannot be converted to JSONArray
我不确定我的思维框架是否错误。无论如何,这是我的代码。如果您需要更多,请告诉我。感谢。
private class validateUser extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
InputStream inputStream = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Logging In...");
progressDialog.show();
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface arg0) {
validateUser.this.cancel(true);
Log.e("Process","Cancelled");
}
});
}
@Override
protected Void doInBackground(String...params) {
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
String baseUrl = "HOST.local";
String token = "oauth/v2/token?";
String client_id = "client_id=CLIENT_ID";
String client_secret = "&client_secret=CLIENT_SECRET";
String grant_type = "&grant_type=password&username=";
String url = baseUrl + token + client_id + client_secret + grant_type + username + "&password=" + password;
httpClient = new DefaultHttpClient();
httpContext = new BasicHttpContext();
response = null;
try {
httpPost = new HttpPost(url);
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
entity = response.getEntity();
inputStream = entity.getContent();
}
catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncodingException", e1.toString());
e1.printStackTrace();
}
catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
}
catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
}
catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
try {
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
}
Log.e("Response", httpPost.toString());
return null;
}
protected void onPostExecute(Void v) {
//parse JSON data
Integer i;
try {
JSONArray jArray = new JSONArray(result);
for(i=0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String access_token = jObject.getString("access_token");
int expires_in = jObject.getInt("expires_in");
String token_type = jObject.getString("token_type");
String scope = jObject.getString("scope");
String refresh_token = jObject.getString("refresh_token");
} // End Loop
this.progressDialog.dismiss();
} catch (JSONException e) {
Log.e("JSONException", "Error: " + e.toString());
} // catch (JSONException e)
} // protected void onPostExecute(Void v)
}