[英]How to extract the access_token from url of Mailchimp
http://127.0.0.1:8080/Mailchimp/Access#access_token=^^^^^^^^^^^^^^^^^^^^^^^^^&expires_in=0
This is my mailchimp url , how to extract the access_token through java 这是我的mailchimp网址,如何通过java提取access_token
and there another way to get the access token but here getting response message - Bad Request 并有另一种获取访问令牌的方法,但此处获取响应消息-错误的请求
URL siteUrl = new URL("https://login.mailchimp.com/oauth2/token");
HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
DataOutputStream out = new DataOutputStream(conn.getOutputStream());
String content = "?grant_type=authorization_code&client_id=^^^^^^^^^^&client_secret=^^^^^^^^^^^^^^^^^^^^^^^^&code="+auth_code+"&redirect_uri=http://127.0.0.1:8080/Mailchimp/Access";
out.writeBytes(content);
System.out.println(content);
out.flush();
out.close();
System.out.println(conn.getResponseMessage());
The part after the # is called the "fragment" or "reference". #后面的部分称为“片段”或“参考”。 This is a basic way to extract the relevant part: 这是提取相关部分的基本方法:
public static String getAccessToken(String uriString) {
URI uri = URI.create(uriString);
String[] parameters = uri.getFragment().split("\\&");
for (String parameter : parameters) {
String[] parts = parameter.split("\\=");
if (parts[0].equals("access_token")) {
if (parts.length == 1) {
throw new RuntimeException("missing access token");
}
return parts[1];
}
}
throw new RuntimeException("no access token");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.