'Send message with Google Business Messages using access token in C#
I am trying to send a google business message in c#, but getting 403 Forbidden error. Below is the code that I have used. Cant figure out what is going wrong.
var token = GetAccessTokenFromJSONKey("D:\\test\\key.json", "https://www.googleapis.com/auth/businessmessages");
string url = "https://businessmessages.googleapis.com/v1/conversations/111d4745-8f28-4c7e-acd1-111234fd/messages";
representative rep = new representative();
rep.avatarImage = "https://developers.google.com/identity/images/g-logo.png";
rep.displayName = "XYZ";
rep.representativeType = "HUMAN";
GooglePostData gd = new GoogleData { text = "TestMessage", messageId = "123456", representative = rep };
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers.Add("Authorization", "Bearer "+ token);
var resp = client.UploadString(url, "POST", JsonConvert.SerializeObject(gd));
}
//AccessToken is obtained from below methods
using (var stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read))
{
return await GoogleCredential
.FromStream(stream) // Loads key file
.CreateScoped(scopes) // Gathers scopes requested
.UnderlyingCredential // Gets the credentials
.GetAccessTokenForRequestAsync(); // Gets the Access Token
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
