0

I have a problem in the exchange of the authorization code for the token (Lifelog API). This is my code:

package daniele.lifel;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {


    WebView myWebView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final RequestQueue queue = Volley.newRequestQueue(this);

        myWebView = (WebView) findViewById(R.id.webview);
        myWebView.clearCache(true);

        myWebView.setWebViewClient(new WebViewClient() {


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                if(url.contains("localhost")) {
                    Log.d("code",url); 
                    Uri uri =  Uri.parse( url );;
                    final String codice=(uri.getQueryParameter("code").toString());
                    Log.d("codice", codice); 





                    url = "https://platform.lifelog.sonymobile.com/oauth/2/token";
                    JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
                            new Response.Listener<JSONObject>() {
                                @Override
                                public void onResponse(JSONObject response) {

                                    try {

                                        String access_token = response.getString("access_token");
                                        Log.d("access_token",access_token);
                                        int expires_in = response.getInt("expires_in");
                                        String token_type = response.getString("token_type");
                                        String refresh_token = response.getString("refresh_token");
                                        int refresh_token_expires_in = response.getInt("refresh_token_expires_in");

                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            },
                            new Response.ErrorListener() {
                                @Override
                                public void onErrorResponse(VolleyError error) {
                                    Log.d("Volley error", error.toString());
                                }
                            }) {
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String>  params = new HashMap<String, String>();
                            params.put("client_id", "fc6badba-f7ef-4fec-a7ce-439563710f4f");
                            params.put("client_secret", "*********");
                            params.put("grant_type", "authorization_code");
                            params.put("code", codice);

                            return params;
                        }
                    };
                    queue.add(jsObjRequest);



                    return true;
                }

                Log.d("My Webview ", url);

                // return true; //Indicates WebView to NOT load the url;
                return false; //Allow WebView to load url
            }
        });



        myWebView.loadUrl("https://platform.lifelog.sonymobile.com/oauth/2/authorize?client_id=fc6badba-f7ef-4fec-a7ce-439563710f4f&scope=lifelog.activities.read");



    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

I obtain the following error:

[25337] BasicNetwork.performRequest: Unexpected response code 401 for https://platform.lifelog.sonymobile.com/oauth/2/token

Any solutions? Thanks

Daniele Oriana
  • 67
  • 1
  • 2
  • 9
  • Try using `getBody` instead of `getParams` – BNK Apr 15 '16 at 13:59
  • Are you able to see the initial Lifelog authorization screen or does the app give you an error right away? Also you should never share your client secret in a public forum. For demonstration purposes you can always replace your client secret with something like "*******". I would recommend going to your Lifelog management console and creating a new set of credentials as soon as possible: https://developer.sony.com/develop/services/lifelog-api/create-app/ – pg316 Apr 15 '16 at 15:53
  • @BNK I'm trying this [link](http://stackoverflow.com/questions/19837820/volley-jsonobjectrequest-post-request-not-working), in particular the answer with the description of getBody() method, but it doesn't work: the error says that encodeParameters() has a private access in Volley – Daniele Oriana Apr 16 '16 at 08:46
  • @Robert-Sony I'm able to see the initial Lifelog authorization screen, the problem is only the exchange of the authorization code for the token through a post request. I have changed the credentials, thank you for the suggestion. – Daniele Oriana Apr 16 '16 at 08:49
  • @DanieleOriana pls try `getBody` as in my answer at http://stackoverflow.com/questions/33573803/how-to-send-a-post-request-using-volley-with-string-body/ – BNK Apr 17 '16 at 15:51
  • @BNK same error with your getBody method : [2543] BasicNetwork.performRequest: Unexpected response code 401 for https://platform.lifelog.sonymobile.com/oauth/2/token – Daniele Oriana Apr 18 '16 at 08:48
  • @DanieleOriana have you tried with some tools such as Postman yet? If success, pls post the screenshot – BNK Apr 18 '16 at 09:11
  • i have solved this problem using a library found on gitHub – Daniele Oriana Apr 23 '16 at 10:53

0 Answers0