我们有一些应用程序(或者我们应该称它们为一小撮脚本)使用Google来简化一些管理任务。最近,在同一个项目中创建了另一个client_id之后,我开始收到一个类似于请求错误)中描述的错误消息。也就是说,
错误400: invalid_request 你不能登录这个应用程序,因为它不符合谷歌的OAuth 2.0政策,以确保应用程序的安全。 您可以让应用程序开发人员知道这个应用程序不符合一个或多个Google验证规则。 请求详细信息: 本节中的内容由应用程序开发人员提供。谷歌尚未对此内容进行审查或验证。 如果您是应用程序开发人员,请确保这些请求细节符合谷歌的政策。 redirect_uri: urn:ietf:wg:oauth:2.0:oob
如何克服这个错误?必须指出的是:
urn:ietf:wg:oauth:2.0:oob
重定向URI (复制/粘贴是在没有浏览器的无头计算机上运行的唯一友好方式)。下面是围绕授权流的代码部分,不同客户端ID的URL是在$stderr.puts url
行上生成的。这与在这里的官方例子中有记载 (本文撰写时的版本)几乎是一样的。
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
def user_credentials_for(scope, user_id = 'default')
token_store = Google::Auth::Stores::FileTokenStore.new(:file => token_store_path)
authorizer = Google::Auth::UserAuthorizer.new(client_id, scope, token_store)
credentials = authorizer.get_credentials(user_id)
if credentials.nil?
url = authorizer.get_authorization_url(base_url: OOB_URI)
$stderr.puts ""
$stderr.puts "-----------------------------------------------"
$stderr.puts "Requesting authorization for '#{user_id}'"
$stderr.puts "Open the following URL in your browser and authorize the application."
$stderr.puts url
code = $stdin.readline.chomp
$stderr.puts "-----------------------------------------------"
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI)
end
credentials
end
发布于 2022-03-27 16:14:05
"Hello“用于处理此错误:
生成身份验证URL
https://github.com/googleapis/google-api-nodejs-client#generating-an-authentication-url
const {google} = require('googleapis');
const oauth2Client = new google.auth.OAuth2(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
// generate a url that asks permissions for Blogger and Google Calendar scopes
const scopes = [
'https://www.googleapis.com/auth/blogger',
'https://www.googleapis.com/auth/calendar'
];
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: scopes
});
如果出了问题,第一步是再次检查google.auth.OAuth2
函数的三个值。
2个中的1个
与Google控制台下的存储值比较
YOUR_CLIENT_ID
YOUR_CLIENT_SECRET
YOUR_REDIRECT_URL
-例如,http://localhost:3000/login
2个(环境变量)中的2个
很多时候,值存储在.env
中。因此,请重新检查env
和文件下的输出-例如index.ts
(甚至使用console.log
)。
.env
# Google Sign-In (OAuth)
G_CLIENT_ID=some_id_1234
G_CLIENT_SECRET=some_secret_1234
PUBLIC_URL=http://localhost:3000
index
const auth = new google.auth.OAuth2(
process.env.G_CLIENT_ID,
process.env.G_CLIENT_SECRET,
`${process.env.PUBLIC_URL}/login`
);
总数:
这样的事是行不通的
const oauth2Client = new google.auth.OAuth2(
"no_such_id",
"no_such_secret",
"http://localhost:3000/i_forgot_to_Authorised_this_url"
);
https://stackoverflow.com/questions/71318804
复制相似问题