Android 如何从YouTube API v3.0更新访问令牌
在我的android应用程序中,我使用Youtube API来喜欢视频。我发出POST请求以获取访问令牌和刷新令牌,但我知道该令牌仅能使用一小时。 所以我想用我的刷新令牌更新访问令牌。我阅读了YouTube API文档,看到了这个POST请求的样子,但不知道它在代码中是如何生成的Android 如何从YouTube API v3.0更新访问令牌,android,youtube-api,http-post,youtube-data-api,google-api-java-client,Android,Youtube Api,Http Post,Youtube Data Api,Google Api Java Client,在我的android应用程序中,我使用Youtube API来喜欢视频。我发出POST请求以获取访问令牌和刷新令牌,但我知道该令牌仅能使用一小时。 所以我想用我的刷新令牌更新访问令牌。我阅读了YouTube API文档,看到了这个POST请求的样子,但不知道它在代码中是如何生成的 private class updateToken extends AsyncTask<String, Void, Void> { @Override protected Void
private class updateToken extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://www.googleapis.com/oauth2/v4/token?"
+"client_id={CLIENT_ID}&"+
"client_secret={CLIENT_SECRET}&"+
"refresh_token={REFRESH_TOKEN}"+
"&grant_type=refresh_token");
//Don't know, I need Bearer or not
httpPost.addHeader("Authorization", "Bearer " + accessToken);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody;
try {
responseBody = httpClient.execute(httpPost, responseHandler);
System.out.println("responseBody: " + responseBody);
} catch (IOException e) {
e.printStackTrace();
};
return null;
}
}
我有这个请求的所有参数,但不知道怎么做,总是看到不同的错误,请帮助我
这似乎是对API的简单POST请求。有什么问题吗?请用您用来更新访问令牌的代码更新问题。这看起来像是对API的简单POST请求。有什么问题吗?请使用您用于更新访问令牌的代码更新问题。
private class refreshToken2 extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://www.googleapis.com/oauth2/v4/token");
httppost.addHeader("content-type","application/x-www-form-urlencoded");
httppost.setHeader("Authorization", "Bearer {AccessToken}");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<>(4);
nameValuePairs.add(new BasicNameValuePair("refresh_token", {RefreshToken});
nameValuePairs.add(new BasicNameValuePair("client_id", "{Myclient_id}");
nameValuePairs.add(new BasicNameValuePair("client_secret", "myclient_secret"));
nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return null;
}
}
"error": "unauthorized_client","error_description": "Unauthorized"