2017-12-12 7 views
0

アプリは、OAuth2リソースオーナーパスワード資格付与のみをサポートするAPIに接続する必要があります。私はコードを使用して試してみましたが、レスポンスコード400の "不正なリクエスト"を取得します。同じコードを使用して、通常のサイトに接続してコンテンツを取得することができます。Android OAuth2リソースオーナーパスワード資格付与

私は、APIコードはポストマン作品を使用しているために動作していることを知っています。郵便配達で、私はちょうど、ユーザー名、パスワード、およびgrant_typeを補給すると、x-www-form-urlencodedで接続の復帰がJSONである

を使用して、ポストの要求を行います。

何が悪いと思いますか?サードパーティライブラリを使用する必要がありますか?どのようなrecomendations?ありがとう。コードで

私は資格証明書とAPIのリンクアモッドで推薦して、いくつかのログを追加

public class GetData extends AsyncTask<String, String, String> { 

@Override 
public String doInBackground(String... args) { 

    URL url; 
    HttpURLConnection urlConnection = null; 
    try { 
     url = new URL("http://someaddress.azurewebsites.net/api/token"); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("username", "[email protected]"); 
     urlConnection.setRequestProperty("password", "123"); 
     urlConnection.setRequestProperty("grant_type", "password"); 

     urlConnection.connect(); 

     int responseCode = urlConnection.getResponseCode(); 
     String responseMsg = urlConnection.getResponseMessage(); 
     if (responseCode >= 400 && responseCode <= 499) { 
      throw new Exception(responseMsg + " :: " + responseCode); 
     } 

     InputStream in = urlConnection.getInputStream(); 
     InputStreamReader isw = new InputStreamReader(in); 

     int data = isw.read(); 
     while (data != -1) { 
      char current = (char) data; 
      data = isw.read(); 
      System.out.print(current); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (urlConnection != null) { 
      urlConnection.disconnect(); 
     } 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(String result) { 

    //Do something with the JSON string 

} 
} 

を変更しました。上記のコードでprintStackTraceが返されます。

12-12 01:16:33.210 8558-8643/com.marcussabino.tsftestedeconexo W/System.err: java.lang.Exception: Bad Request :: 400 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at com.marcussabino.tsftestedeconexo.GetData$override.doInBackground(GetData.java:40) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at com.marcussabino.tsftestedeconexo.GetData$override.access$dispatch(GetData.java) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at com.marcussabino.tsftestedeconexo.GetData.doInBackground(GetData.java:0) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at com.marcussabino.tsftestedeconexo.GetData.doInBackground(GetData.java:15) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at android.os.AsyncTask$2.call(AsyncTask.java:295) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
12-12 01:16:33.211 8558-8643/com.marcussabino.tsftestedeconexo W/System.err:  at java.lang.Thread.run(Thread.java:818) 
+1

あなたにもログをアップロードしてくださいすることができます –

答えて

0

私はsetRequestPorpertyを間違って理解していました。

おかげネルと衝撃を与える:ここではhttps://stackoverflow.com/a/40576153/4276115

が私のために働いていたコードは次のとおりです。

public class GetData extends AsyncTask<String, String, String> { 

@Override 
public String doInBackground(String... args) { 
    String urlParameters = "username=" + args[0] + 
      "&password=" + args[1] + 
      "&grant_type=" + args[2]; 

    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); 
    int postDataLength = postData.length; 

    URL url; 
    HttpURLConnection urlConnection = null; 
    try { 
     url = new URL("http://somesite.azurewebsites.net/api/token"); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setDoOutput(true); 
     urlConnection.setDoInput(true); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     urlConnection.setRequestProperty("charset", "utf-8"); 
     urlConnection.setRequestProperty("Content-Length", Integer.toString(postDataLength)); 
     urlConnection.setUseCaches(false); 

     try(DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream())) { 
      wr.write(postData); 
     } 

     int responseCode = urlConnection.getResponseCode(); 
     String responseMsg = urlConnection.getResponseMessage(); 
     if (responseCode >= 400 && responseCode <= 499) { 
      throw new Exception(responseMsg + " :: " + responseCode); 
     } 

     InputStream in = urlConnection.getInputStream(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(in)); 

     StringBuffer buffer = new StringBuffer(); 
     String line = ""; 

     while ((line = reader.readLine()) != null) { 
      buffer.append(line+"\n"); 
      Log.v("JSON", "> " + line); //here you get the response 

     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } finally { 
     if (urlConnection != null) { 
      urlConnection.disconnect(); 
     } 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(String result) { 

    //Do something with the JSON string 

} 
} 
関連する問題