我在交换令牌(Lifelog API)的授权代码时遇到问题。这是我的代码:
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);
}
}
我收到以下错误:
[25337] BasicNetwork.performRequest: https ://platform.lifelog.sonymobile.com/oauth/2/token 的意外响应代码 401
有什么解决办法吗?谢谢