尝试发送推送通知时出现 Firebase 403 禁止错误
Firebase 403 Forbidden Error on attempting to send Push Notification
我有一个完全可用的旧版 HTTP 请求:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Headers.Add("Authorization", "key=" + key);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string message = "{\"to\":\"" + token.ToString() +
"\",\"notification\":{\"body\":\"" + body.ToString() +
"\",\"title\":\"" + title.ToString() + "\"}," +
"\"data\":{\"key\":\"" + dataKey.ToString() +
"\",\"value\":\"" + dataValue.ToString() + "\"}}";
streamWriter.Write(message);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
name = streamReader.ReadToEnd();
}
我正在尝试迁移到最新版本:
string uri = String.Format("https://fcm.googleapis.com/v1/projects/{0}/messages:send?access_token={1}&key={2}&alt=json", project, AccessToken, key);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string message = "{\"validateOnly\":true," +
"\"message\":{" +
"\"token\":\"" + token.ToString() + "\"," +
"\"notification\":{" +
"\"body\":\"" + body.ToString() + "\"," +
"\"image\":\"" + image.ToString() + "\"," +
"\"title\":\"" + title.ToString() + "\"" +
"},\"data\":{" +
"\"key\":\"" + dataKey.ToString() + "\"," +
"\"value\":\"" + dataValue.ToString() + "\"" +
"}" +
"}" +
"}";
streamWriter.Write(message);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
name = streamReader.ReadToEnd();
}
但是在 GetResponse
我收到 The remote server returned an error: (403) Forbidden.
我之前收到 401 错误(未授权)但那是因为我没有使用正确的访问密钥,我现在已经获得授权使用这个:
var scopes = new[] { "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database" };
GoogleCredential credential;
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}
AccessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return AccessToken;
}
我真的不确定我错过了什么,我已经为这个项目启用了所有必要的 Google 云 API。
(403) Forbidden
表示您已通过身份验证(您的令牌有效),但您无权访问 URL
.
中的资源
向 oauth 方法添加另一个范围。 "https://www.googleapis.com/auth/firebase.messaging"
现在看起来很简单,但是这个 c# post 没有完整的位置。希望这可以帮助其他人使用这种方法。
var scopes = new[] { "https://www.googleapis.com/auth/firebase.messaging", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database" };
GoogleCredential credential;
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}
AccessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
我有一个完全可用的旧版 HTTP 请求:
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("https://fcm.googleapis.com/fcm/send");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
httpWebRequest.PreAuthenticate = true;
httpWebRequest.Headers.Add("Authorization", "key=" + key);
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string message = "{\"to\":\"" + token.ToString() +
"\",\"notification\":{\"body\":\"" + body.ToString() +
"\",\"title\":\"" + title.ToString() + "\"}," +
"\"data\":{\"key\":\"" + dataKey.ToString() +
"\",\"value\":\"" + dataValue.ToString() + "\"}}";
streamWriter.Write(message);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
name = streamReader.ReadToEnd();
}
我正在尝试迁移到最新版本:
string uri = String.Format("https://fcm.googleapis.com/v1/projects/{0}/messages:send?access_token={1}&key={2}&alt=json", project, AccessToken, key);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Accept = "application/json";
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string message = "{\"validateOnly\":true," +
"\"message\":{" +
"\"token\":\"" + token.ToString() + "\"," +
"\"notification\":{" +
"\"body\":\"" + body.ToString() + "\"," +
"\"image\":\"" + image.ToString() + "\"," +
"\"title\":\"" + title.ToString() + "\"" +
"},\"data\":{" +
"\"key\":\"" + dataKey.ToString() + "\"," +
"\"value\":\"" + dataValue.ToString() + "\"" +
"}" +
"}" +
"}";
streamWriter.Write(message);
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
name = streamReader.ReadToEnd();
}
但是在 GetResponse
我收到 The remote server returned an error: (403) Forbidden.
我之前收到 401 错误(未授权)但那是因为我没有使用正确的访问密钥,我现在已经获得授权使用这个:
var scopes = new[] { "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database" };
GoogleCredential credential;
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}
AccessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();
return AccessToken;
}
我真的不确定我错过了什么,我已经为这个项目启用了所有必要的 Google 云 API。
(403) Forbidden
表示您已通过身份验证(您的令牌有效),但您无权访问 URL
.
向 oauth 方法添加另一个范围。 "https://www.googleapis.com/auth/firebase.messaging"
现在看起来很简单,但是这个 c# post 没有完整的位置。希望这可以帮助其他人使用这种方法。
var scopes = new[] { "https://www.googleapis.com/auth/firebase.messaging", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/firebase.database" };
GoogleCredential credential;
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}
AccessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync();