应用程序需要谷歌刷新令牌,我尝试调用基于谷歌OAuth2文档的服务。
https://developers.google.com/identity/protocols/oauth2installedApp
步骤5:交换刷新和访问令牌的授权代码
“java.io.FileNotFoundException:https://www.googleapis.com/oauth2/v4/token”
使用服务,例如:
POST/oauth2/v4/token HTTP/1.1主机:www.googleapis.com内容-类型:application/x-www-form-urlencoded
code=4/p7q7w91a-omscelviaqm6btrgtp7&client_id=your_client_id&client_secret=your_client_secret&redirect_uri=https://oauth2.example.com/code&grant_type=authorization_code
示例代码:
class getTokenAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String JsonResponse = "";
BufferedReader reader = null;
HttpURLConnection urlConn;
try {
URL url = new URL("https://www.googleapis.com/oauth2/v4/token");
String client_id = "******9-tt3qav2d2rp45sgjqp5helrsbvc22kdq.apps.googleusercontent.com";
String client_secret = ""; //This value is not needed for clients registered as Android, iOS, or Chrome applications.
String redirect_uri = "http://localhost";
String grant_type = "authorization_code";
String code = params[0]; // serverAuthCode code
HashMap<String, String> params1 = new HashMap<String, String>();
params1.put("client_id", client_id);
params1.put("client_secret", client_secret);
params1.put("grant_type", grant_type);
params1.put("redirect_uri", redirect_uri);
params1.put("code", code); // your code received
Set set = params1.entrySet();
Iterator i = set.iterator();
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, String> param : params1.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null; ) {
builder.append(line).append("\n");
}
reader.close();
conn.disconnect();
System.out.println("token returned: " + builder.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
请建议我,如何调用服务和收到刷新令牌id在Android应用程序。
我创建了一个新文件,但得到以下错误: java.io.FileNotFoundException:C:\users\xxx\ap\metadata\cb_2015-09-01_07-31-01.ini(Sistem belirtilen Dosyay ̄Bulam ̄Yor)在java.io.fileInputStream.Open0(本机方法)在java.io.fileInputStream.Ope
我们的堆栈使用主干作为客户端应用程序,使用Spring Boot作为RESTful API。 我们尝试使用OAuth2进行基本身份验证,用户提供用户名和密码。 我们使用Spring Security进行身份验证,使用jQuery$. ajax方法进行请求。但是,在我们甚至可以使用带有我们的秘密的POST标头进行授权之前,我们得到的响应是预飞行OPTIONS请求的401(未授权)状态。但是,我们可以
我正在尝试用单节点hadoop集群运行一个示例程序。我在运行示例程序(jar文件)时遇到一个异常。 多谢了。
map 0%reduce 0%15/02/03 07:30:28信息MapReduce.job:任务Id:尝试_1422885720829_0097_M_000000_0,状态:失败错误:java.lang.RuntimeException:java.lang.ClassNotFoundException:Class org.cnognizant.pr2.troublemapper在org.apa
我的问题很简单,我已经请求了JSONYouTubev3数据API,但是,我想显示从链接请求的数据:https://www.googleapis.com/youtube/v3/channels?part=statistics