gpt4 book ai didi

perl - 使用 LWP::Authen::OAuth2 访问受 OAuth2 保护的 Google API 时出现问题

转载 作者:行者123 更新时间:2023-12-01 00:04:02 24 4
gpt4 key购买 nike

我目前在 perl 服务器上编写服务,该服务将向 Firebase Cloud Messaging API 发送请求,然后将推送通知发送到应用程序实例。

由于 FCM 是 Google API 系列的一部分,因此需要 OAuth2 token 才能访问 API。在我的研究中,我发现 this perl 解决方案。因为我的服务是在非谷歌服务器环境中运行的,所以我不能使用谷歌应用程序默认凭据,而是必须手动提供,所以我下载了一个包含私钥的 json this描述。

阅读documentation of LWP::Authen::OAuth2我有点困惑,将 json 中的哪个参数放入 $oauth2 的位置对象,因为通常使用不同的名称来引用相同的值,就像我怀疑的那样。

与我的firebase项目相关的json:

{
"type": "service_account",
"project_id": "my_project_id",
"private_key_id": "some_key_id",
"private_key": "-----BEGIN PRIVATE KEY-----very_long_key-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-o8sf4@<my_project_id>.iam.gserviceaccount.com",
"client_id": "some_client_id",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-o8sf4%40<my_project_id>.iam.gserviceaccount.com"
}
$oauth的执行对象看起来像这样:
my $oauth2 = LWP::Authen::OAuth2->new(
client_id => "Public from service provider",
#probably that will be "some_client_id" from above

client_secret => "s3cr3t fr0m svc prov",
#the "very_long_key"?

service_provider => "Google",
#the "auth_uri"? That's what I would suggest here
#I've read some about the LWP::Authen::OAuth2::ServiceProvider module
#do I have to create an instance of that here?
#if so, which params do I need for that from the json?

redirect_uri => "https://your.url.com/",
#the FCM api I want to call?

# Optional hook, but recommended.
save_tokens => \&save_tokens,
save_tokens_args => [ $dbh ],

# This is for when you have tokens from last time.
token_string => $token_string.

#yes, i copy-pasted that from the docs
);

现在,作为 Perl 的初学者和不喜欢模棱两可的键值名称的人,我有点困惑,将哪个值放在哪里,如果有人可以在这里帮助我提供指南,我会很高兴,即使这个应该放在哪里似乎是一个非常菜鸟的问题,这对我来说很重要:D。所以我感谢每一个有用的答案!

编辑

当试图生成 JSON 网络 token 在我的 perl 服务中手动使用 Crypt::JWT ,我遇到了另一个绊脚石,这让我怀疑 Google 提供的相应身份验证 API "https://www.googleapis.com/auth/firebase.messaging"仍然接受不记名 token ......我尝试生成我的 JWT,这似乎是成功的,但是我发送到实际 FCM API 的请求然后给了我这个:
Request had invalid authentication credentials. 
Expected OAuth 2 access token, login cookie
or other valid authentication credential

在打印为 String 的响应中,我找到了这个小家伙,这让我很困惑:
Client-Warning: Unsupported authentication scheme 'bearer'

现在我很不确定,FCM API 仍然支持承载 token ,即使它们在引用 docs page 的示例中使用。 .有没有人有这方面的最新信息?非常感谢你!

最佳答案

深入研究各种文档并测试我意识到的一些事情,LWP::Authen::OAuth2 不仅有点开销,对于创建一个非常小的 HTTPS 请求到 OAuth2 保护的 API,目前也是不可能的。
警告隐藏在 service_provider ,谁托管身份验证 API,我必须调用它来验证和授权我的服务以访问实际的 Firecase 云消息传递 API。就我而言,这是 Google,更准确地说是 https://www.googleapis.com/auth/firebase.messaging ,也称为 scope在代码中。
现在,一个服务提供商可以为不同的客户端提供不同的身份验证 API。这就是为什么一些服务提供商需要一个额外的参数 - client_typetype分别(为明确起见,我将使用 client_type ) - 这也会影响通过 OAuth2 的身份验证和授权过程。
当创建一个新的 $oauth2 -object,并为字段 service_provider 赋值模块的对象LWP::Authen::OAuth2::ServiceProvider被创建,可能还需要它的参数,比如 scope , 来定义您希望获得授权的 API 系列,并取决于 client_type .
现在谷歌不是一个无名的服务提供者,所以已经有一个预构建的服务提供者模块,明确地用于谷歌 API:LWP::Authen::OAuth2::ServiceProvider::Google .这会自动填充 ServiceProvider 的一些参数对象,并持有可用 client_types 的哈希值确保您使用其中之一,因为取决于 client_type ServiceProvider::Google 的特定子模块在内部创建。
所以我试着像这样测试:

my $oauth2 = LWP::Authen::OAuth2->new(
service_provider => "Google",
scope => "https://www.googleapis.com/auth/firebase.messaging",
client_type => "service_account", #referring to 'type' in the json
client_id => "Public from service provider",
client_secret => "s3cr3t fr0m svc prov",
redirect_uri => "this-server.mydomain.com"
);
按照进一步的描述 here并使用内置 LWP::UserAgent 对象发送请求我仍然收到 401 错误 UNAUTHENTICATED这让我很困惑。因此,当我阅读文档时,我不知道是什么时候跨过 client_types 中的这条细线。 ServiceProvider::Google章节:

Service Account

This client_type is for applications that login to the developer's account using the developer's credentials. See https://developers.google.com/accounts/docs/OAuth2ServiceAccount for Google's documentation.

This is not yet supported, and would require the use of JSON Web Tokens to support.


是的,这有点破坏了整个 LWP:Authen::OAuth 的使用。家庭访问 Firebase API,因为几乎所有这些 API 都有 client_type => "service_account" .现在我的项目有一个截止日期,我迫不及待地想要扩展模块,或者我自己扩展它会超出我的 perl 技能。
但在绝望的泪水之间总有一丝希望,正如我发现的 this Google 文档页面有一个活生生的附录,可以使用 JSON 网络 token 作为不记名 token 。为此,我发现不止一个 Perl 解决方案可以从 JSON 生成 JWT,比如服务帐户,还有 this非常有用的 Stackoverflow 答案,向我展示了摆脱瓶颈的方法。

关于perl - 使用 LWP::Authen::OAuth2 访问受 OAuth2 保护的 Google API 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55455958/

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