Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 无法在Google OAuth2.0中对服务器到服务器应用程序进行身份验证_Node.js_Typescript_Google Cloud Platform_Google Cloud Storage_Google Oauth - Fatal编程技术网

Node.js 无法在Google OAuth2.0中对服务器到服务器应用程序进行身份验证

Node.js 无法在Google OAuth2.0中对服务器到服务器应用程序进行身份验证,node.js,typescript,google-cloud-platform,google-cloud-storage,google-oauth,Node.js,Typescript,Google Cloud Platform,Google Cloud Storage,Google Oauth,我正在尝试设置GoogleOAuth身份验证,以便获得帐户服务的访问令牌,并通过RESTAPI(如云存储)使用其他服务。 问题是,当我发出访问令牌请求时,我得到一个400错误,消息是通用的,所以我不知道什么不起作用。 我正在使用Typescript、Node和一些包(比如jsonwebtokens或jwtsimple)对jwt进行签名 如果我遗漏了什么,有什么想法吗 import moment from 'moment'; import jwt from 'jwt-simple'; import

我正在尝试设置GoogleOAuth身份验证,以便获得帐户服务的访问令牌,并通过RESTAPI(如云存储)使用其他服务。 问题是,当我发出访问令牌请求时,我得到一个400错误,消息是通用的,所以我不知道什么不起作用。 我正在使用Typescript、Node和一些包(比如jsonwebtokens或jwtsimple)对jwt进行签名

如果我遗漏了什么,有什么想法吗

import moment from 'moment';
import jwt from 'jwt-simple';
import jsonwebtoken from 'jsonwebtoken'; // try wuth this one for signing the JWT too but same result
import axios from 'axios';
import base64Url from 'base64url';

export const authentication = function (): void {
    const nowInUnix = moment().unix();
    const secret = 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCc9HWEZ2VtoIt5...........';
    //this's supposed to be my private key so I removed the beginning and end from it 'cause in the file from where i get it was like this: "private_key": "-----BEGIN PRIVATE KEY-----MY_KEY-----END PRIVATE KEY-----\n"

    let header = { "alg": "RS256", "typ": "JWT" };
    const headerEncoded = base64Url.encode(JSON.stringify(header));
    let claims = {
        "iss": "service-account@my-project.iam.gserviceaccount.com",
        "scope": "https://www.googleapis.com/auth/devstorage.full_control",
        "aud": "https://oauth2.googleapis.com/token",
        "exp": nowInUnix + 2000,
        "iat": nowInUnix
    };
    const claimsEncoded = base64Url.encode(JSON.stringify(claims));
    // console.log(claimsEncoded);
    const stringToEncrypt = headerEncoded + '.' + claimsEncoded;
    // console.log(stringToEncrypt);
    const signature = jwt.encode(stringToEncrypt, secret);
    // console.log(signature);
    const fullToken = headerEncoded + '.' + claimsEncoded + '.' + signature;
    console.log(fullToken);

    // Something else i don't know if how to pass the params, I assumed it's like query params but I also tried sending the in the body request and same result.
    axios.post(`https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${fullToken}`)
        .then(res => console.log('todo ok'))
        .catch(e => console.log(e));
}
这是我得到的错误: 错误请求:400

谷歌文档我读到:

我根据第一条注释修复了代码,现在它是这样工作的:

import moment from 'moment';
import jsonwebtoken from 'jsonwebtoken';
import axios from 'axios';

export const authentication = function (): void {
    const nowInUnix = moment().unix();
    const secret = "-----BEGIN PRIVATE KEY-----YOUR_KEY-----END PRIVATE KEY-----\n";
    let claims = {
        "iss": "service-account@my-project.iam.gserviceaccount.com",
        "scope": "https://www.googleapis.com/auth/devstorage.full_control",
        "aud": "https://oauth2.googleapis.com/token",
        "exp": nowInUnix + 3600,
        "iat": nowInUnix
    };
    const token = jsonwebtoken.sign(claims, secret, { algorithm: 'RS256'});
    const response: any = await axios.post(`https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${token}`)
        .catch(e => console.log('Error occured: \n' + e));
    console.log(response.data);
}
答复:

{
  access_token: 'MY_TOKEN',
  expires_in: 3599,
  token_type: 'Bearer'
}

1) 您正在生成错误的签名类型(HS256)。使用RS256
jwt.encode(有效负载,机密,'RS512')
。2) 函数
jwt.encode
返回整个令牌(头、负载和签名),而不仅仅是签名。3) 不要剥夺私钥。4) 一旦获得签名的JWT,就必须交换访问令牌。阅读本文了解如何:非常感谢您的帮助!我根据你的评论修改了代码,最终成功了。我使用jsonwebtoken包对RS512进行编码。我只将
claims
对象传递给signing方法,并包含了所有私钥字符串,现在它工作得很好。再次感谢!