【发布时间】:2019-04-01 13:10:26
【问题描述】:
我目前正在 perl 服务器上编写一个服务,该服务将向 Firebase Cloud Messaging API 发送一个请求,该 API 然后将推送通知发送到一个应用实例。
由于 FCM 是 Google API 系列的一部分,因此需要 OAuth2 令牌才能访问 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。所以我很感谢每一个有用的答案!
编辑
当尝试使用 Crypt::JWT 在我的 perl 服务中手动生成 JSON Web 令牌 时,我遇到了另一个绊脚石,这让我怀疑来自 Google "https://www.googleapis.com/auth/firebase.messaging" 的相应身份验证 API 仍然接受不记名令牌...我尝试生成我的 JWT,这似乎是成功的,但是我发送到实际 FCM API 的请求然后给了我这个:
Request had invalid authentication credentials.
Expected OAuth 2 access token, login cookie
or other valid authentication credential
在打印为字符串的响应中,我发现了这个小家伙,这让我很困惑:
Client-Warning: Unsupported authentication scheme 'bearer'
现在我非常不确定,FCM API 仍然支持不记名令牌,即使它们在引用 docs page 的示例中使用。有没有人有这方面的最新信息?非常感谢!
【问题讨论】:
-
太棒了,因为我被一只美丽的独角兽分心了,很多闪光我忘了添加所有链接...稍后会跟进:D - 添加链接
-
我稍微分解了我的不记名令牌问题,以保持问题的可读长度。要获取更多详细信息和代码 sn-ps,请参阅此问题:stackoverflow.com/questions/55494788/…
标签: perl oauth-2.0 google-api firebase-cloud-messaging lwp