gpt4 book ai didi

google-cloud-messaging - FCM授权总是失败

转载 作者:行者123 更新时间:2023-12-05 03:10:54 25 4
gpt4 key购买 nike

今天我想从 GCM 切换到 FCM,所以我设置了所需的一切并希望实现服务器端代码。我用了gcm4j库并将其更改为地址转到 https://fcm.googleapis.com/fcm/send

所以我在做以下事情:

FCM fcm = new FCMDefault(new FCMConfig().withKey(FCMGlobals.FCM_API_KEY));

FCMRequest request = new FCMRequest().withRegistrationId(android.getRegistration())
// .withCollapseKey(collapseKey)
.withDelayWhileIdle(true)
.withDataItem(FCMGlobals.FCM_PARAM_CODE, code)
.withDataItem(FCMGlobals.FCM_PARAM_USER_ID, "" + user.getId())
.withDataItem(FCMGlobals.FCM_PARAM_ADDITION, "" + addition);

ListenableFuture<FCMResponse> responseFuture = fcm.send(request);

Futures.addCallback(responseFuture, new FutureCallback<FCMResponse>() {

public void onFailure(Throwable t) {
log.error(t);
}

@Override
public void onSuccess(FCMResponse response) {
log.info(response.toString());
}
});

它的实现是:

protected FCMResponse executeRequest(FCMRequest request) throws IOException {
byte[] content = this.objectMapper.writeValueAsBytes(request);

HttpURLConnection conn = this.connectionFactory.open(this.fcmUrl);
conn.setRequestMethod("POST");
conn.addRequestProperty("Authorization", getAuthorization(request));
conn.addRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setFixedLengthStreamingMode(content.length);

LoggerFactory.getLogger("FCMDefaultAbstract").info("Authorization: " + conn.getRequestProperty("Authorization"));
LoggerFactory.getLogger("FCMDefaultAbstract").info("Content-Type: " + conn.getRequestProperty("Content-Type"));
LoggerFactory.getLogger("FCMDefaultAbstract").info("send: " + new String(content));

try (OutputStream outputStream = conn.getOutputStream()) {
IOUtils.write(content, outputStream);
} catch (Exception e) {
throw new FCMNetworkException("Error sending HTTP request to FCM", e);
}

FCMResponse response;

try (InputStream inputStream = conn.getInputStream()) {
response = this.objectMapper.readValue(IOUtils.toByteArray(inputStream), FCMResponse.class);
} catch (IOException e) {
try (InputStream inputStreamError = conn.getErrorStream()) {
String str = inputStreamError != null ? IOUtils.toString(inputStreamError) : "No error details provided";
int responseCode = conn.getResponseCode();

if (responseCode < 500) {
throw new FCMNetworkException(conn.getResponseCode(), str.trim(), e);
} else {
throw new FCMNetworkException(conn.getResponseCode(), str.trim(), checkForRetryInResponse(conn), e);
}
}
}

response.setRequest(request);
response.setRetryAfter(checkForRetryInResponse(conn));

Iterator<String> iteratorId = request.getRegistrationIds().iterator();
Iterator<FCMResult> iteratorResponse = response.getResults().iterator();

while (iteratorId.hasNext() && iteratorResponse.hasNext()) {
iteratorResponse.next().setRequestedRegistrationId(iteratorId.next());
}

if (iteratorId.hasNext()) {
LOG.warn("Protocol error: Less results than requested registation IDs");
}

if (iteratorResponse.hasNext()) {
LOG.warn("Protocol error: More results than requested registation IDs");
}

return response;
}

这里是日志输出:

FCMDefaultAbstract                      Authorization: null
FCMDefaultAbstract Content-Type:application/json
FCMDefaultAbstract send: {"registration_ids":["dMpvzp*************************************2lRsSl_5lFET2"],"data":{"CODE":"201","USER_ID":"1","ADDITION":"1468083549493"},"delay_while_idle":true}
FCM FCMNetworkException: HTTP 401: No error details provided

Authorization header 实际上不为空。它已使用我的 FCM API key 正确设置。如果有人尝试访问 Authorization key ,只有 HTTPUrlConnection 实现会返回 null。

如您所见,我无法连接 FCM。代码401表示认证失败。

这可能是什么问题?

最佳答案

检查您使用的是服务器类型 API-KEY,而不是客户端浏览器 API-KEY。

如果您使用的是 Firebase,您可以在
中找到 API-KEY项目设置 > 云消息传递

如果您正在使用云控制台,或者您不确定您使用的是哪个 key ,您可以通过 https://console.cloud.google.com 生成新 key

引用文档
https://firebase.google.com/docs/cloud-messaging/concept-options#credentials

Server key: A server key that authorizes your app server for access to Google services, including sending messages via Firebase Cloud Messaging. [...]
Important: Do not include the server key anywhere in your client code. Also, make sure to use only server keys to authorize your app server. Android, iOS, and browser keys are rejected by FCM.

关于google-cloud-messaging - FCM授权总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38284209/

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