如何在google auth请求中对返回的uri进行编码?

我正在尝试使用google auth API来实现一个小应用程序,但是我基本上被困在了第一步。 我所有的代码如下:

var google = require("googleapis"); var OAuth2 = google.auth.OAuth2; const encodedUrl = encodeURIComponent("http://localhost:3000/oauthcallback"); var oauth2Client = new OAuth2( "YOU CAN'T SEE THIS", "THIS IS ALSO SECRET", encodedUrl ); const getLoginUrl = () => { const url = oauth2Client.generateAuthUrl({ // 'online' (default) or 'offline' (gets refresh_token) access_type: "offline", // If you only need one scope you can pass it as a string scope: [ "https://www.googleapis.com/auth/admin.directory.orgunit.readonly", "https://www.googleapis.com/auth/userinfo.profile" ] }); return url; }; let url = getLoginUrl(); url; 

这个代码的计划是它应该给我我需要进行身份validation的代码,它的确如此,但它不起作用。 出于某种原因我得到:

错误:invalid_request

redirect_uri的无效参数值:Uri必须包含可打印的> ASCII字符: http:// localhost:3000 / oauthcallback

有任何想法吗? 我已经尝试了URL编码,而不是使用encodeURIComponent ,但两种情况都有相同的结果。