gpt4 book ai didi

oauth-2.0 - Force.com OAuth 2.0 JWT 到 Google 服务帐户融合表 API 400 错误请求 Invalid_Grant

转载 作者:行者123 更新时间:2023-12-03 08:03:13 25 4
gpt4 key购买 nike

我想要完成的是能够将数据从 Force.com 上传到已设置的服务帐户下的 Google Fusion Table 中,以便可以根据该数据生成网络图可视化,在 Force.com 的 VF 页面中显示。

我编写了一个包含以下内容的 Apex 类:

public class NetworkGraphTestController {
public static String base64URLEncode(Blob input) {
String output = encodingUtil.base64Encode(input);
output = output.replace('+', '-');
output = output.replace('/', '_');
while (output.endsWith('=')) {
output = output.subString(0, output.length()-1);
}
return output;
}

public static Long findSecondsBetween2DateTimes(DateTime dt1, DateTime dt2)
{
Integer intDays = dt1.Date().daysBetween(dt2.Date());
Long seconds = dt2.getTime() - dt1.getTime();
Long daysToSeconds = intDays * 24 * 60 * 60;
return daysToSeconds + seconds;
}

public static void generateJWT() {

String pkCS8PrivateKey = 'PRIVATE KEY OBTAINED VIA OPENSSL';

DateTime utc0 = DateTime.newInstance(1970, 1, 1, 0, 0, 0);
DateTime issueTime = DateTime.now();

JSONGenerator gen = JSON.createGenerator(false);
gen.writeStartObject();
gen.writeStringField('iss', 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com');
gen.writeStringField('scope', 'https:\\/\\/www.googleapis.com\\/auth\\/fusiontables');
gen.writeStringField('aud', 'https:\\/\\/accounts.google.com\\/o\\/oauth2\\/token');
Long now = findSecondsBetween2DateTimes(utc0, issueTime);
gen.writeNumberField('exp', now + 3500);
gen.writeNumberField('iat', now);
String claimSet = gen.getAsString().trim();
System.debug(gen.getAsString());
System.debug('Gen trimmed: ' + gen.getAsString().trim());

String header = '{"alg":"RS256", "typ":"JWT"}';
String signatureInput = base64URLEncode(blob.valueOf(header)) + '.' + base64URLEncode(blob.valueOf(claimSet));

Blob signature = Crypto.sign('RSA', blob.valueOf(signatureInput), encodingUtil.base64decode(pkCS8PrivateKey));

String jwt = signatureInput+'.'+base64URLEncode(signature);

//System.debug(jwt);

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
req.setMethod('POST');
req.setBody('grant_type=' + encodingUtil.urlEncode('urn:ietf:params:oauth:grant-type:jwt-bearer', 'UTF-8')+'&assertion=' + encodingUtil.urlEncode(jwt, 'UTF-8'));
req.setEndpoint('https://accounts.google.com/o/oauth2/token');
System.debug('Request: ' + req + ' BODY: ' + req.getBody());
//System.debug(req.toString());
HttpResponse res = h.send(req);
System.debug(res + ' BODY: ' + res.getBody() + ' STATUS: ' + res.getStatus());
}
}

已设置服务帐户,并已在 Google API 控制台中启用 Fusion Tables API。我从 Google 通过 OpenSSL 提供的 PKCS12 p12 私钥文件中提取了 PKCS8 key ,虽然我不熟悉这个过程,所以可能是犯了一个错误。我注意到的是,通过我多次尝试提取该私钥中的值,Crypto 类不接受 key 值,直到我获得当前存储在 pkCS8PrivateKey 中的值。如果我没记错的话,它首先将 PKCS12 文件转换为中间 pem 文件,然后提取 PKCS8 格式的私钥,该私钥输出到另一个 pem 文件中。

我遇到的 400 'Bad Request' 错误正文如下:

{
"error" : "invalid_grant"
}

我也尝试过不转义 iss 和范围参数中的正斜杠,但这没有区别。我还允许访问“https://accounts.google.com” ' 通过远程站点安全设置。

我编写这段代码的最大来源是这个问题: Force.com Apex Code to generate Google API oAuth 2.0 JWT

该问题的创建者最终与我现在处于同一条船上,因为他只能在尝试通过 Google 进行身份验证时检索到 invalid_grant 错误。不幸的是,他的问题没有得到解答,所以,我来了。

对于我所面临的问题所提供的任何帮助,我表示感谢。

最佳答案

不幸的是,我不确定这是否可以使用 Apex。我创建了一个测试 Java 程序(针对 Google API)来观察 Java 生成的内容与 Apex 之间的差异。我注意到两者生成的签名不同,这将其缩小到 Crypto.sign() 方法的输出。

我找到了 this提供以下信息的链接:

The Apex Crypto class provides support for Digital Signatures with the sign() method. The following considerations apply:

  • The two algorithms are RSA and RSA-SHA1, which are functionally equivalent.
  • A PKCS8 formatted private key in base64 decoded form is required. This private key should not be hardcoded in the Apex script but should be stored in a protected custom setting or a encrypted fields in a custom table.
  • It is equivalent to the Java Signature.sign() class method using "SHA1withRSA".
  • In C#, it is the equivalent of (1) signing the clear text using SHA1Managed.ComputeHash() and (2) Signing using RSACryptoServiceProvider.ComputeHash() against the resulting hash.
  • Functionally, it will compute a SHA1 digest from clear text and encrypt the digest using RSA with the provided private key.

我在这里强调了关键问题,我相信您需要与 SHA256withRSA 等效的东西,它似乎不是 Crypto 类的一个选项(至少我不知道)。

因此,总而言之,我认为您的代码是正确的,但生成的签名不是。

关于oauth-2.0 - Force.com OAuth 2.0 JWT 到 Google 服务帐户融合表 API 400 错误请求 Invalid_Grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17238848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com