使用OAuth2登录到PHP应用程序
我目前有一个基于PHP的web应用程序(使用使用OAuth2登录到PHP应用程序,php,android,json,symfony,oauth-2.0,Php,Android,Json,Symfony,Oauth 2.0,我目前有一个基于PHP的web应用程序(使用FOSUserbundle和FOSOAuthServerBundle使用Symfony2构建)。我目前正在尝试让我的服务器验证试图从其移动设备(Android)登录的用户,并发回访问令牌和刷新令牌 理想情况下,结果是:用户将其规范用户名和密码输入客户端(移动应用程序),用户通过身份验证,服务器使用访问令牌和刷新令牌进行响应。用户可以访问仅与他们相关的信息。(我不确定隐式授权或密码流在这里是否更有意义) 到目前为止,我已经成功地从服务器接收到一个响应,但
FOSUserbundle
和FOSOAuthServerBundle
使用Symfony2
构建)。我目前正在尝试让我的服务器验证试图从其移动设备(Android)登录的用户,并发回访问令牌
和刷新令牌
理想情况下,结果是:用户将其规范用户名和密码输入客户端(移动应用程序),用户通过身份验证,服务器使用访问令牌和刷新令牌进行响应。用户可以访问仅与他们相关的信息。(我不确定隐式授权或密码流在这里是否更有意义)
到目前为止,我已经成功地从服务器接收到一个响应,但是我总是得到一个JSON响应,声明:Invalid Request。更准确地说:
Error:org.json.JSONException:Value{“Error\u description”:“无效的授权类型参数或参数丢失”,“Error”:“无效的请求”}类型org.json.JSONObject无法转换为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)
}
私有类validateUser扩展异步任务{
private ProgressDialog ProgressDialog=新建ProgressDialog(LoginActivity.this);
InputStream InputStream=null;
字符串结果=”;
受保护的void onPreExecute(){
progressDialog.setMessage(“登录…”);
progressDialog.show();
progressDialog.setOnCancelListener(新的DialogInterface.OnCancelListener(){
public void onCancel(对话框接口arg0){
validateUser.this.cancel(true);
Log.e(“过程”、“取消”);
}
});
}
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串username=loginUsername.getText().toString();
字符串密码=loginPassword.getText().toString();
String baseUrl=“HOST.local”;
字符串标记=“oauth/v2/token?”;
字符串client\u id=“client\u id=client\u id”;
字符串client\u secret=“&client\u secret=client\u secret”;
字符串grant_type=“&grant_type=password&username=”;
字符串url=baseUrl+token+client_id+client_secret+grant_type+username+“&password=“+password;
httpClient=新的DefaultHttpClient();
httpContext=新的BasicHttpContext();
响应=空;
试一试{
httpPost=新的httpPost(url);
nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“用户名”,username));
添加(新的BasicNameValuePair(“密码”,password));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
response=httpClient.execute(httpPost);
entity=response.getEntity();
inputStream=entity.getContent();
}
捕获(不支持DencodingException e1){
Log.e(“UnsupportedEncodingException”,e1.toString());
e1.printStackTrace();
}
捕获(客户端协议异常e2){
Log.e(“ClientProtocolException”,e2.toString());
e2.printStackTrace();
}
捕获(非法状态例外e3){
Log.e(“IllegalStateException”,e3.toString());
e3.printStackTrace();
}
捕获(IOE4异常){
Log.e(“IOException”,e4.toString());
e4.printStackTrace();
}
试一试{
BufferedReader bReader=新的BufferedReader(新的InputStreamReader(inputStream,“iso-8859-1”),8);
StringBuilder sBuilder=新StringBuilder();
字符串行=null;
而((line=bReader.readLine())!=null){
sBuilder.append(第+行“\n”);
}
inputStream.close();
结果=sBuilder.toString();
}捕获(例外e){
Log.e(“StringBuilding&BufferedReader”,“错误转换结果”+e.toString());
}
Log.e(“Response”,httpPost.toString());
返回null;
}
受保护的void onPostExecute(void v){
//解析JSON数据
整数i;
试一试{
JSONArray jArray=新JSONArray(结果);
对于(i=0;i
您正在设置查询参数和POST数据,然后作为POST请求发送到服务器。如果您发出post请求,服务器是否会处理您的查询参数?您是否尝试过使用查询参数发送GET请求?@atastrumf我尝试过POST和GET命令。两者都返回了相同的响应。