5

Tôi đang cố gắng lấy mã thông báo truy cập từ máy chủ bằng cách sử dụng yêu cầu Chuỗi vô tuyến. Tôi đã cố gắng thực hiện một JsonObjectRequest cũng. Cả hai đều dưới đây.Cuộc gọi trên WebAPI bằng cách sử dụng số điện thoại

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", "[email protected]"); 
       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", "[email protected]"); 
     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); 

.

tôi nhận được câu trả lời sau đây từ máy chủ:

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

.

Đồng nghiệp của tôi đã viết mã phía máy chủ đã hỏi cách chuyển đổi mã Angular sau (mã của anh ấy hoạt động với API), sang Android.

Có ai có thể trợ giúp việc này không?

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

Trả lời

5

Vấn đề là một vài điều.

Tôi đã thay thế "params.put (" tài nguyên "," window.location.origin ");" với "params.put (" tài nguyên "," https://lw.xxx.co.uk ");"

Ngoài ra, tôi phát hiện ra rằng Volley bỏ qua ghi đè getHeaders, vì vậy tôi đã nhận xét phương thức đó và sử dụng phần sau để đặt tiêu đề.

@Override 
      public String getBodyContentType() { 
       return "application/x-www-form-urlencoded"; 
      } 

public void getAuthenticationTokens(Object param1, final String userName, final String password, final CustomListener<JSONObject> listener) 
    { 
     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 
      public String getBodyContentType() { 
       return "application/x-www-form-urlencoded"; 
      } 

      @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", "https://lw.xxx.co.uk"); 
       params.put("grant_type", "password"); 
       params.put("username", userName); 
       params.put("password", password); 
       return params; 
      } 


      @Override 
      protected VolleyError parseNetworkError(VolleyError response) { 
       try { 

        String json = new String(response.networkResponse.data, HttpHeaderParser.parseCharset(response.networkResponse.headers)); 
        Log.e(TAG, "reponse error = " + json); 
       }catch (Exception e){} 
       return super.parseNetworkError(response); 
      } 
     }; 
     requestQueue.add(request); 

    }//end of getAuthenticationTokens 
+0

Rất vui khi thấy bạn đã sửa lỗi này. Xin hãy giải thích cho chúng tôi vấn đề gì? –

+1

@IntelliJAmiya Xin cảm ơn. Có lỗi, tôi đã cập nhật những gì tôi đã làm trên một bình luận cho một câu trả lời. tiếc là người đó đã xóa bài đăng của anh ấy, vì vậy nhận xét của tôi cũng biến mất. Tôi đã cập nhật câu trả lời của mình ngay bây giờ. cảm ơn – turtleboy

Các vấn đề liên quan