Send file/image as a message from local system in Microsoft Team Channel - microsoft-graph-api

How can I send the local file/image in Microsoft Teams Channel using Send Message of Microsoft Graph API?
I have followed a document https://learn.microsoft.com/en-us/graph/api/resources/chatmessageattachment?view=graph-rest-1.0 to attach an image when creating a message to send the team channel. I am trying to send the base64 format of an image. But I am getting an error.
{
"error": {
"code": "InternalServerError",
"message": "Failed to process request.",
"innerError": {
"date": "2020-09-25T11:43:02",
"request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
}

It seems there is no appropriate API for you to send local file as message directly.
Creating chatMessage in a channel just supports send the file that has been in SharePoint. So, there are two steps to send the local file: first, upload file to SharePoint, then create chatMessage.
1. Upload file to SharePoint:
File smaller than 4MB:
PUT https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/content
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)
Upload a file using select file option
file-name: is file name along with extension example: test.txt
File greater than 4MB:
GET: https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/createUploadSession
Header:
"Authorization" : "Bearer <access-token>"
The Get request will return upload url.
PUT <upload url>
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)
For more details, see here.
2. Create chatMessage:
POST https://graph.microsoft.com/v1.0/teams/{id}/channels/{id}/messages
Content-type: application/json
{
"body": {
"contentType": "html",
"content": "Here's the latest budget. <attachment id=\"153fa47d-18c9-4179-be08-9879815a9f90\"></attachment>"
},
"attachments": [
{
"id": "153fa47d-18c9-4179-be08-9879815a9f90",
"contentType": "reference",
"contentUrl": "https://m365x987948.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx",
"name": "Budget.docx"
}
]
}

Related

Send email for Graph Api works in Postman but not through App in Azure?

I am accessing the Graph Api endpoint:
https://graph.microsoft.com/v1.0/me/sendMail
I retrieve the access token from Graph Explorer and use it as a Bearer token in Postman.
I also set Content-Type in the header to "application/json".
I set the Body as
{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [
{
"emailAddress": {
"address": "testuser#acme.com"
}
}
]
}
}
Through Graph Explorer I have given consent for Mail.Send
It works fine in Postman. I set the scope in my c# app as Mail.Send, but when i send the request it gives the error
{StatusCode: 403, ReasonPhrase: 'Forbidden'
What other permission do i need to set ?

Unsupported Media Type - 415 error in POST request in Graph Explorer

I am trying out Graph Explorer. I am able to get my profile from https://graph.microsoft.com/v1.0/me/.
But when I try to create a device using the Graph Explorer, it is returning Unsupported Media Type - 415 response.
My request and response are:
Request
POST https://graph.microsoft.com/v1.0/devices
Request headers
Content-type: application/json
Request body
{
"accountEnabled": false,
"alternativeSecurityIds": [
{
"type": 3,
"key": "base64Y3YxN2E1MWFlYw=="
}
],
"deviceId": "4c299165-6e8f-4b45-a5ba-c5d250a707ff",
"displayName": "Test device",
"operatingSystem": "linux",
"operatingSystemVersion": "1"
}
I have given the consent for all permissions under Modify permissions section.
Response
Unsupported Media Type - 415 - 1168ms
{
"error": {
"code": "Request_BadRequest",
"message": "The specified content type 'application/json;odata=minimalmetadata, application/json' contains either no media type or more than one media type, which is not allowed. You must specify exactly one media type as the content type.",
"innerError": {
"request-id": "11235ffc-1fb3-4324-a832-2c92274e98bf",
"date": "2020-04-06T16:55:17"
}
}
}
Edit
value of deviceId in the request data
"DeviceId" AND "Key" that you are passing in Request body should be Unique identifier. Change you sample Device-Id & Key and try again. It worked for me. and you also increased the length of device id by appending 1 at end.
Documentation for accountEnabled here.

Forbidden when accessing image/sticker urls in chat messages returned from Graph Api

I received "Forbidden" when accessing an image/sticker link in a chat message return from Microsoft Graph Api.
I received the following chat message from Graph Api (not all properties are not listed):
{
"body": {
"contentType": "html",
"content": "<div><div>Message 499\n<div><span><img src=\"https://graph.microsoft.com/beta/teams/82d87df3-b8bd-4bdc-9090-5f8666b32134/channels/19:baf66ad0b75f4656bf20ff5667fae565#thread.skype/messages/1562258187781/hostedContents/aWQ9eF8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMSx0eXBlPTEsdXJsPWh0dHBzOi8vdXMtYXBpLmFzbS5za3lwZS5jb20vdjEvb2JqZWN0cy8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMS92aWV3cy9pbWdv/$value\" width=\"250\" height=\"250\" style=\"vertical-align:bottom; width:250px; height:250px\"></span>\n\n</div>\n\n\n</div>\n</div>"
}
}
Then I tried to access the image link
GET https://graph.microsoft.com/beta/teams/82d87df3-b8bd-4bdc-9090-5f8666b32134/channels/19:baf66ad0b75f4656bf20ff5667fae565#thread.skype/messages/1562258187781/hostedContents/aWQ9eF8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMSx0eXBlPTEsdXJsPWh0dHBzOi8vdXMtYXBpLmFzbS5za3lwZS5jb20vdjEvb2JqZWN0cy8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMS92aWV3cy9pbWdv/$value
Headers:
Authorization: bearer [access_token]
The response was:
403 Forbidden
{
"error": {
"code": "Forbidden",
"message": "Forbidden",
"innerError": {
"request-id": "3143682e-4dd2-4057-ad83-e4d7fbf32b98",
"date": "2019-07-05T07:09:06"
}
}
}
Headers:
request-id: 3143682e-4dd2-4057-ad83-e4d7fbf32b98
client-request-id: 3143682e-4dd2-4057-ad83-e4d7fbf32b98
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"Southeast Asia","Slice":"SliceC","Ring":"3","ScaleUnit":"001","RoleInstance":"AGSFE_IN_2","ADSiteName":"SEA"}}
Date: Fri, 05 Jul 2019 07:09:05 GMT
However, I was able to list the hosted contents of the same channel:
GET: https://graph.microsoft.com/beta/teams/82d87df3-b8bd-4bdc-9090-5f8666b32134/channels/19:baf66ad0b75f4656bf20ff5667fae565#thread.skype/messages/1562258187781/hostedContents
Headers:
Authorization: bearer [access_token]
Response
{
"#odata.context": "https://graph.microsoft.com/beta/$metadata#teams('82d87df3-b8bd-4bdc-9090-5f8666b32134')/channels('19%3Abaf66ad0b75f4656bf20ff5667fae565%40thread.skype')/messages('1562258187781')/hostedContents",
"#odata.count": 1,
"value": [
{
"id": "aWQ9eF8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMSx0eXBlPTEsdXJsPWh0dHBzOi8vdXMtYXBpLmFzbS5za3lwZS5jb20vdjEvb2JqZWN0cy8wLWN1cy1kOC0xYjlkYTQ2NzA1ZTI5YjA5N2QzMDgyZjc2ODZiYTlhMS92aWV3cy9pbWdv"
}
]
}
Also, I was able to retrieve the image from another chat message in another channel (the general channel) in the same team.
GET https://graph.microsoft.com/beta/teams/82d87df3-b8bd-4bdc-9090-5f8666b32134/channels/19:db70de74054944fe9dbce0a592eddb6d#thread.skype/messages/1562252008138/hostedContents/aWQ9eF8wLWN1cy1kNi1iYWY0OTc0ZGNjMjc0MTAwMjI5YmMyZjcyY2Y0ODk2NSx0eXBlPTEsdXJsPWh0dHBzOi8vdXMtYXBpLmFzbS5za3lwZS5jb20vdjEvb2JqZWN0cy8wLWN1cy1kNi1iYWY0OTc0ZGNjMjc0MTAwMjI5YmMyZjcyY2Y0ODk2NS92aWV3cy9pbWdv/$value
Headers:
Authorization: bearer [access_token]
The user associated with the access token is one of the owners of the team, but he is not the one posting the chat message containing the image.
What am I missing here?
Update: I did more testing and found the following pattern.
The issue did not happen for General channel.
For other channels, if a user had not posted any message to a channel, accessing the image link of the chat messages resulted in Forbidden error. After the user posted one message, it could access the link fine.
Looks like a permission synchronization issue. Any idea?

image upload failure from Gupshup chatbot through whatsapp channel

I am using this
var image = {"type":"image","originalUrl":"image url here","previewUrl":"thumbnail url here"};
context.sendResponse(JSON.stringify(image));
format for uploading image to gupshup whatsapp chatbot, but image is not been send,
the error it is showing is
{
"meta": {
"api_status": "stable",
"version": "2.21.4"
},
"errors": [
{
"code": 1014,
"title": "Internal error",
"details": "Image file format (application/octet-stream) is not supported"
}
]
}
There is no support for sending the image file as Content-Type application/octet-stream by WhatsApp hence you got the error.
Supported Content-Types are image/jpeg, image/png. Hence, you will have to provide Image URL which ends with image extension .jpg/.jpeg/.png

Attempting to create an issue with JIRA REST API results in a 500 error

For automation purposes I'm trying to create issues through the JIRA REST API, but I'm getting a 500, Internal Server Exception as response without any additional information.
With the Chrome-plugin "advance REST client", I ran through the following steps:
Entered the endpoint: h.ttps://[company-jira-url]/rest/api/2/issue/
Set the HTTP method: POST
Added the required headers:
Content-Type: application/json
Authorization: Basic (100% sure this one's correct)
Added the json payload (Basically the minimum required fields):
{
"fields": {
"project":
{
"key": "HACK"
},
"summary": "REST EXAMPLE",
"description": "Creating an issue via REST API",
"issuetype": {
"name": "Bug"
}
}
}
Click "Send"
The request basically looks like this (base64 string is not correct in this example):
The Request
Result:
The Response
Anyone ?

Resources