我应该在Firebase Cloud Messaging测试中使用什么Bearer令牌?

son*_*lis 8 firebase-cloud-messaging

我正在尝试通过Postman使用Firebase Cloud Messaging发送测试通知。我正在对此网址进行POST

https://fcm.googleapis.com/v1/projects/[my project name]/messages:send
Run Code Online (Sandbox Code Playgroud)

邮递员中的“授权”选项卡设置为“无身份验证”,“我的标题”选项卡如下所示

Content-Type: application/json
Authorization: Bearer [server key]
Run Code Online (Sandbox Code Playgroud)

[服务器密钥]是Firebase项目“设置”区域中“云消息”标签中的新生成的服务器密钥。我不断收到此错误作为回应。

"error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
}
Run Code Online (Sandbox Code Playgroud)

根据我能找到的所有信息,我使用了正确的令牌,但Google似乎不同意。我应该发送什么作为Authorization标头来克服此错误?

Ram*_*mis 30

获取Authentication Bearer的步骤:

  1. 到 Google OAuth Playground:https : //developers.google.com/oauthplayground
  2. 在FCM的“输入您自己的范围”中使用此网址:https : //www.googleapis.com/auth/firebase.messaging
  3. 点击授权 API。
  4. 选择正确的用户进行授权并允许访问。
  5. 第 2 步:为令牌交换授权码中,点击为令牌交换授权码
  6. 访问令牌是您的Bearer

发送 FCM 抛出 Postman 的步骤:

  1. 发送网址:https : //fcm.googleapis.com/v1/projects/projectid-34543/messages :send
  2. 请求类型:POST
  3. 标头:Content-Type -> application/json & Authorization -> Bearer
  4. 在正文部分,输入带有正确设备令牌的 APS 负载。
  5. 点击发送。

在此处输入图片说明

如果您想使用 cURL,对于数据通知:

curl --location --request POST 'https://fcm.googleapis.com/v1/projects/your-project-id/messages:send' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer your-access-token-*****-wqewe' \
--data-raw '{
    "message": {
        "token": "device-token-qwfqwee-***-qefwe",
        "data": {
            "Key1": "val1",
            "Key2": "val2"
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

  • 天哪,gif 图像让人很难理解 (6认同)
  • 如何以编程方式获取代码(在后端)? (2认同)

Yos*_*har 9

不记名令牌是使用您的 firebase 服务帐户获取 OAuth 访问令牌的结果。

  1. 为自己获取一个 Firebase 服务帐户密钥。
    转到您的Firebase 控制台> 设置 > 服务帐户。
    如果您在 Firebase Admin SDK 上生成新的私钥。

  2. 您可以使用服务帐号密钥对自己进行身份验证并获取不记名令牌。
    在此处遵循如何在 Node、Python 或 Java 中执行此操作:https : //firebase.google.com/docs/cloud-messaging/auth-server

因此,在 Java 中,您可以像这样获取令牌:

  private static final String SCOPES = "https://www.googleapis.com/auth/firebase.messaging";

  public static void main(String[] args) throws IOException {
    System.out.println(getAccessToken());
  }

  private static String getAccessToken() throws IOException {
    GoogleCredential googleCredential = GoogleCredential
        .fromStream(new FileInputStream("service-account.json"))
        .createScoped(Arrays.asList(SCOPES));
    googleCredential.refreshToken();
    return googleCredential.getAccessToken();
  }
Run Code Online (Sandbox Code Playgroud)
  1. 现在您终于可以使用 FCM 发送您的测试通知了。

邮递员代码:

POST /v1/projects/[projectId]/messages:send HTTP/1.1
Host: fcm.googleapis.com
Content-Type: application/json
Authorization: Bearer access_token_you_just_got

{
  "message":{
    "token" : "token_from_firebase.messaging().getToken()_inside_browser",
    "notification" : {
      "body" : "This is an FCM notification message!",
      "title" : "FCM Message"
      }
   }
}
Run Code Online (Sandbox Code Playgroud)


dem*_*nYu 8

您必须在Postman中生成新的访问令牌。

首先,请确保您已在Google Developer Console中启用了FCM API。比转到Google开发者控制台-> API和服务->凭据。查看“ OAuth 2.0客户端ID”部分。列表中至少应有一项。将其下载为json文件。

在Postman中打开“授权”选项卡,选择Type =“ OAuth 2.0”,然后单击“获取新访问令牌”。出现对话框。

领域:

令牌名称-输入所需内容

赠款类型=授权码

回调URL =来自下载的json的redirect_uris

验证网址= auth_uri

访问令牌URL = token_uri

客户编号= client_id

客户端机密= client_secret

范围=“ https://www.googleapis.com/auth/firebase.messaging

状态-留空

客户端身份验证=默认,作为基本身份验证标头发送

单击“请求令牌”,仅此而已。


Apo*_*orv 6

要生成用于测试的推送通知,您可以使用Google Developers OAuth 2.0 Playground

您甚至可以使用 Google Developers OAuth 2.0 Playground 本身发送测试推送通知。 或者,如果您愿意,也可以使用邮递员/终端(curl 命令)。

请在此处找到我写的 详细步骤。在此输入图像描述

注意:您必须使用“项目 ID ”,而不是端点中的“项目名称”。上面的链接中还提到了获取项目 ID 的步骤。