Android和Symfony2的OAuth2密码流(FOSOAuthServerBundle)

时间:2015-02-14 17:13:38

标签: php android symfony oauth-2.0 fosoauthserverbundle

我试图允许用户使用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();

我使用此Password Flow Client by Daniel Szmulewicz aka danielsz来管理我的令牌。

logcat的:

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)

如果您需要更多信息,请与我们联系。谢谢你的帮助。

0 个答案:

没有答案