1

我正在编写一个需要 Google API 并使用 OAuth2 作为身份验证机制的小型 node.js Web 应用程序。在请求访问令牌时,我注意到了一些特殊情况——允许访问将类型从对象更改为字符串后收到的“代码”。响应类型记录在“建筑物中的 OAuth”之后。

对不起,如果这是一个重复的问题。

路线:

handle['/connect'] = requestHandlers.connect;
handle['/oauth2callback'] = requestHandlers.oauth;

路由器:

console.log("About to route a request for " + pathname);

if (typeof handle[pathname] === 'function'){
    handle[pathname](response);
    if (pathname === '/oauth2callback'){
        handle[pathname](query);
    }
} else {
    console.log("No request handler found for " + pathname);
    response.writeHead(404, {"Content-Type": "text/plain"});
    response.write("404 Not found");
    response.end();
}

处理程序:

function connect(response){
    console.log("Request Handler 'start' was called.");
    auth.access(response);
}

function oauth(response){
    console.log("OAuth in the building. This is the instance: ");
    if(typeof response !== 'undefined'){
        if(typeof response == "string"){
            var code = response.split("code=").pop();
            auth.exchange(code);
        }else {
        console.log("This response isn't ready until it is a string. It has these values: " + Object.getPrototypeOf(response));
        }
    }
}

验证:

function getAccess(response){
    var url = authClient.generateAuthUrl({
        access_type: 'offline',
        scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.readonly'
    });
    console.log("[Auth.js] This is the response received: " + Object.keys(response));
    response.writeHead(302,{
        "Location":url
    });
    response.end();
}

function exchangeToken(code){
    authClient.getToken(code,function(err,tokens){
        console.log("We got some tokens! " + tokens);
    });

    console.log("Authorized");
}

在此处输入图像描述

4

0 回答 0