【发布时间】:2015-12-31 08:50:13
【问题描述】:
我在 SO 中进行了很多搜索,浏览了许多关于同一错误的问题,应用了他们建议的任何内容,但结果一无所获。所以写到这里。
我正在学习如何调用 Google API(比如 Google Calendar API)。我正在浏览谷歌开发者教程 https://developers.google.com/identity/protocols/OAuth2ServiceAccount
所以我在 google 中创建了一个服务帐户,创建了凭据(我想从我自己的应用程序中调用 Google API)。之后,我创建了 JWT,使用 google 共享的私钥对我的 JWT 进行了签名,并进行了 REST POST 调用以获取 oAuth。这是我的代码
public class TestGoogleAPI {
public static void main(String[] args) throws Exception{
String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}";
long time = Calendar.getInstance().getTimeInMillis()/1000;
String claimSet = "{\"iss\":\"xxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com\"," +
"\"scope\":\"https://www.googleapis.com/auth/calendar\"," +
"\"aud\":\"https://www.googleapis.com/oauth2/v3/token\"," +
"\"exp\":"+ (time+3600) +"," +
"\"iat\":" + time +"}";
String base64Header = new String(Base64.encodeBase64(header.getBytes()),"UTF-8");
String base64ClaimSet = new String(Base64.encodeBase64(claimSet.getBytes()),"UTF-8");
String input = base64Header + "." + base64ClaimSet;
File f = new File("D:/downloads/privatekey.txt");
FileInputStream fis = new FileInputStream(f);
DataInputStream dis = new DataInputStream(fis);
byte[] keyBytes = new byte[(int)f.length()];
dis.readFully(keyBytes);
dis.close();
byte[] key = Base64.decodeBase64(keyBytes);
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(key);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privatekey = kf.generatePrivate(spec);
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(privatekey);
signature.update(input.getBytes());
String base64Sign = new String(Base64.encodeBase64(signature.sign()),"UTF-8");
doHttpPost("https://www.googleapis.com/oauth2/v3/token", input + "." + base64Sign);
}
public static void doHttpPost(String urlStr, String assertion) throws Exception{
URL url = new URL(urlStr);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream out = conn.getOutputStream();
Writer writer = new OutputStreamWriter(out, "UTF-8");
writer.write("grant_type");
writer.write("=");
writer.write(URLEncoder.encode("urn:ietf:params:oauth:grant-type:jwt-bearer", "UTF-8"));
writer.write("&");
writer.write("assertion");
writer.write("=");
writer.write(URLEncoder.encode(assertion, "UTF-8"));
writer.close();
out.close();
if (conn.getResponseCode() != 200) {
System.out.println(conn.getResponseMessage());
}
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
conn.disconnect();
System.out.println(sb.toString());
}
}
我收到 400(错误请求)。我从“Chrome 的 Advanced Rest Client”中尝试了相同的方法,但结果是相同的,无效的 grant_type(错误请求)。
{
error: "invalid_grant"
error_description: "Bad Request"
}
我在这里有什么遗漏吗?请帮帮我
谢谢
【问题讨论】:
-
查看此链接developers.google.com/analytics/devguides/config/mgmt/v3/…,可能是因为您的服务器时钟与网络时间协议不同步。
-
另外,尝试将您的范围和 URL 更改为 accounts.google.com/o/oauth2/token 而不是 googleapis.com/oauth2/v3/token
-
@SGC 我不确定如何将我的桌面与 NTP 同步。基本上我尝试将我的机器时区更改为 PST(和其他)并尝试相同,但没有结果。我得到了同样的结果。如果我使用 java 程序而不是 REST 调用 API,一切正常。根据我的要求,我需要使用 REST
标签: java oauth google-api google-calendar-api google-oauth