dsyct08008 2015-02-05 15:48
浏览 37

使用OAuth2登录PHP应用程序

I currently have a PHP web-based application (built with Symfony2 using FOSUserbundle and FOSOAuthServerBundle). I'm currently trying to have my server authenticate the user attempting to log in from their mobile device (Android) and send back an access token and a refresh token.

Ideally, the outcome is: The user enters their canonical username and password into the client (mobile app), the user is authenticated, the server responds with an access token and a refresh token. The user gains access to information that only pertains to them. (I'm not certain if an implicit grant or a password flow makes more sense here)

So far, I've managed to receive a response from the server, but I always get a JSON response stating: Invalid Request. To be more precise:

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

I'm not sure if my frame of thinking is wrong or not. In any case, here's my code. Let me know if you need more. Thanks.

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 + "
");
            }

            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)

}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料