![](/img/trans.png)
[英]Implement OAuth2 with resource owner password credentials on Android
[英]Android OAuth2 Resource Owner Password Credentials Grant
該應用程序需要連接到僅支持OAuth2資源所有者密碼憑據授予的API。 我嘗試使用下面的代碼,但得到響應代碼400“錯誤的請求”。 使用相同的代碼,我可以連接到普通站點並檢索內容。
我知道API代碼可以正常工作,因為使用Postman可以正常工作。 在Postman中,我只是發出一個發布請求,提供用戶名,密碼和grant_type,並使用x-www-form-urlencoded
連接的返回是一個json。
任何想法有什么問題嗎? 我應該使用第三方圖書館嗎? 有什么建議嗎? 謝謝。
在代碼中,我更改了憑據和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", "some@email.com");
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
}
}
根據Amod的建議添加一些日志。 這是上面代碼中的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)
我在理解setRequestPorperty錯誤!
謝謝Nell和Stunner: 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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.