ユーザーが FOSOAuthServerBundle を使用して RESTful API にアクセスできるようにしようとしています。ユーザーが Android を使用して API にアクセスできるようにする必要があります。上記のユーザーは、パスワード フローを使用して API に接続する必要があります。
現在、URI でブラウザーを使用する場合:
http://BASE_URL/oauth/v2/token?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&grant_type=password&username=USERNAME&password=PASSWORD
JSON 応答で返されます。
{"access_token":"ACCESS_TOKEN","expires_in":3600,"token_type":"bearer","scope":"user","refresh_token":"REFRESH_TOKEN"}
したがって、サーバーがトークンを返すことができることはわかっています。ただし、OAuth2 クライアントを使用して Android でトークンをリクエストしようとすると、エラーが発生します。私のコードは次のとおりです。
// LoginActivity
loginBtnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
token = null;
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
OAuth2Client client = new OAuth2Client(username, password, CLIENT_ID, CLIENT_SECRET, BASE_URL);
token = client.getAccessToken();
Log.d("Token", token.toString());
//Log.d("Other Token", );
TODO: Add on PostExecute Intent, once Access Token is received and User is verified.
return null;
}
};
task.execute();
Daniel Szmulewicz aka danielsz によるこのPassword Flow Client を使用してトークンを管理しています。
ログキャット:
02-14 11:02:15.849 16396-16396/com.myapp.myproject I/InputMethodManager﹕ [startInputInner] EditorInfo { packageName=com.myapp.myproject, inputType=0x90001, imeOptions=0x8004005, privateImeOptions=null }, windowGainingFocus=android.view.ViewRootImpl$W@3df06de, mServedView=android.widget.AutoCompleteTextView{7c1c0bf VFED..CL .F....ID 0,0-984,118 #7f0b005c app:id/loginUsername}, mServedInputConnectionWrapper=android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper@28f3738c
02-14 11:02:19.459 16396-16396/com.myapp.myproject I/InputMethodManager﹕ [startInputInner] EditorInfo { packageName=com.myapp.myproject, inputType=0x81, imeOptions=0x4001006, privateImeOptions=null }, windowGainingFocus=null, mServedView=android.widget.EditText{1a02a824 VFED..CL .F.P..ID 0,118-984,236 #7f0b005d app:id/loginPassword}, mServedInputConnectionWrapper=android.view.inputmethod.InputMethodManager$ControlledInputConnectionWrapper@1234bf8d
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet+,hn 24(0x696b6170756c2e),sn(),hints(known),family 0,flags 4
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet-, err=8
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet+,hn 24(0x696b6170756c2e),sn(),hints(known),family 0,flags 1024
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet-, pass to proxy
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfo_proxy+
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfo_proxy get netid:0
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfo_proxy-, success
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet+,hn 14(0x32332e3232362e),sn(),hints(known),family 0,flags 4
02-14 11:02:33.539 16396-17247/com.myapp.myproject D/libc﹕ [NET] android_getaddrinfofornet-, SUCCESS
02-14 11:02:34.929 16396-17247/com.myapp.myproject I/System.out﹕ Authorization server expects Basic authentication
02-14 11:02:34.929 16396-17247/com.myapp.myproject I/System.out﹕ encodedBytes Um9tZW86a3NvdHlj
02-14 11:02:34.939 16396-17247/com.myapp.myproject I/System.out﹕ decodedBytes Caesar:passPass
02-14 11:02:34.939 16396-17247/com.myapp.myproject I/System.out﹕ Retry with login credentials
02-14 11:02:36.229 16396-17247/com.myapp.myproject I/System.out﹕ Retry with client credentials
02-14 11:02:36.239 16396-17247/com.myapp.myproject I/System.out﹕ encodedBytes NV8yOHgxcGRnY2VxZjRjdzR3NDR3azg4a2s4a3Njd3M0Z2d3ODRzb29nc3M0Y29zODB3czo0Nnpnd3I4YXV2b2s0d2t3Zzhzc2NrdzhzbzRrOGNnczRvbzQ4ODRzczBvYzAwd2MwNA==
02-14 11:02:36.249 16396-17247/com.myapp.myproject I/System.out﹕ decodedBytes 5_28x1pdgceqf4cw4w44wk88kk8kscws4ggw84soogss4cos80ws:46zgwr8auvok4wkwg8ssckw8so4k8cgs4oo4884ss0oc00wc04
02-14 11:02:37.659 16396-17247/com.myapp.myproject E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.myapp.myproject, PID: 16396
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Could not retrieve access token for user: Caesar
at com.myapp.myproject.library.Oauth2_Password.OAuthUtils.getAccessToken(OAuthUtils.java:158)
at com.myapp.myproject.library.Oauth2_Password.OAuth2Client.getAccessToken(OAuth2Client.java:48)
at com.myapp.myproject.Authentication.LoginActivity$1$1.doInBackground(LoginActivity.java:117)
at com.myapp.myproject.Authentication.LoginActivity$1$1.doInBackground(LoginActivity.java:108)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
さらに情報が必要な場合はお知らせください。ご協力いただきありがとうございます。