WebAPI使用凌空调用

时间:2017-11-10 16:40:26

标签: android asp.net-web-api asp.net-web-api2 .net-core android-volley

我正在尝试使用volley String请求从服务器访问令牌。我也尝试过制作JsonObjectRequest。两者都在下面。

public void getAuthenticationTokens(Object param1, final CustomListener<String> listener)
    {

        //String url = prefixURL + "this/request/suffix";
        String url = "https://lw.xxx.co.uk/connect/token";


        StringRequest request = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>()
                {
                    @Override
                    public void onResponse(String response) {
                        // response
                        Log.e("Response", response);
                    }
                },


                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // error
                        Log.e("Error.Response", error.networkResponse.toString());
                    }
                }
        ) {



            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                //..add other headers
                return params;
            }

            @Override
            protected Map<String, String> getParams()
            {
                Map<String, String>  params = new HashMap<String, String> ();
                params.put("scope", "openid email phone profile offline_access roles");
                params.put("resource", "window.location.origin");
                params.put("grant_type", "password");
                params.put("username", "support@xxx.com");
                params.put("password", "tempPxxx");


                return params;
            }

        };

        requestQueue.add(request);

public void getAuthenticationTokens(Object param1, final CustomListener<String> listener)
    {

        //String url = prefixURL + "this/request/suffix";
        String url = "https://lw.xxx.co.uk/connect/token";

Map<String, Object> jsonParams = new HashMap<>();

        jsonParams.put("scope", "openid email phone profile offline_access roles");
        jsonParams.put("resource", "window.location.origin");
        jsonParams.put("grant_type", "password");
        jsonParams.put("username", "support@xxx.com");
        jsonParams.put("password", "tempPxxx");

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
                new Response.Listener<JSONObject>()
                {
                    @Override
                    public void onResponse(JSONObject response)
                    {
                        Log.d(TAG + ": ", "somePostRequest Response : " + response.toString());
                        if(null != response.toString())
                            listener.getResult(response);
                    }
                },

                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error)
                    {
                        if (null != error.networkResponse)
                        {

                            Log.e(TAG + ": ", "Error Response code: " + error.networkResponse.statusCode);



                            listener.getResult(null);
                        }
                    }
                }){
                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {
                       // Map<String,String> params =  super.getHeaders();
                       // if(params==null)params = new HashMap<>();
                        Map<String,String> params = new HashMap<>();
                        params.put("Content-Type","application/x-www-form-urlencoded");
                        //..add other headers
                        return params;
                    }
        };



        requestQueue.add(request);

我从服务器得到以下响应:

E/Volley: [31388] BasicNetwork.performRequest: Unexpected response code 400 for https://lw.xxx.co.uk/connect/token

我编写服务器端代码的同事已经询问如何将以下Angular代码(他的代码与API一起使用)转换为Android。

任何人都可以帮忙吗?

getLoginEndpoint(userName: string, password: string): Observable<Response> {

        let header = new Headers();
        header.append("Content-Type", "application/x-www-form-urlencoded");

        let searchParams = new URLSearchParams();
        searchParams.append('username', userName);
        searchParams.append('password', password);
        searchParams.append('grant_type', 'password');
        searchParams.append('scope', 'openid email phone profile offline_access roles');
        searchParams.append('resource', window.location.origin);

        let requestBody = searchParams.toString();

        return this.http.post(this.loginUrl, requestBody, { headers: header });
    }

1 个答案:

答案 0 :(得分:5)

问题有两个。

我替换了“params.put(”resource“,”window.location.origin“);”with“params.put(”resource“,”https://lw.xxx.co.uk“);”

另外,我发现Volley忽略了getHeaders覆盖,所以我评论了该方法并使用以下方法来设置标题。

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName