Microsoft Graph REST API v1.0: Can't expand image posted in Teams channel

Viktor Povazhuk 0 Reputation points
2023-10-05T08:44:51.2466667+00:00

Services: Microsoft Graph REST API v1.0, python, Microsoft Teams for business.

Scenario: I used Microsoft Graph REST API v1.0 in python script to post image into Microsoft Teams private channel. The code snippet is attached.

Result: The image is posted. However, I can't expand it: when I click on image in the channel message, nothing happens. How can I fix this problem?

Environment:

OS: Windows 10

Python version: 3.11.4

Troubleshooting efforts:

  1. Reviewed documentation page and confirmed that request destination url and body format haven't updated.
  2. Tried to change resolution of image in attributes height and width in img tag.

Supporting materials:

  • Minimal reproducible example
import json
import os
import base64
import cv2
import requests

def get_token(client_id, client_secret, tenant_id, user_name, password):
    uri = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    body = {'tenant': tenant_id,
            'client_id': client_id,
            'grant_type': 'password',
            'username': user_name,
            'password': password,
            'client_secret': client_secret,
            'scope': "https://graph.microsoft.com/ChannelMessage.Send"
            }

    r = requests.post(uri, data=body, headers=headers)
    return r.json()


def send_image(self, img, token, team_id, channel_id):
    img_bytes = cv2.imencode('.png', img)[1]
    img_bytes = base64.b64encode(img_bytes).decode("utf-8")
    endpoint = f'https://graph.microsoft.com/v1.0//teams/{team_id}/channels/{channel_id}/messages'
    msg = {
        "body": {
            "contentType": "html",
            "content": f"<div><div>\n<div><span><img height=\"1080\" src=\"../hostedContents/1/$value\" width=\"1620\" style=\"vertical-align:bottom; width:1620; height:1080px\"></span>\n\n</div>\n\n\n</div></div>"
        },
        "hostedContents": [
            {
                "@microsoft.graph.temporaryId": "1",
                "contentBytes": img_bytes,
                "contentType": "image/png"
            }
        ]
    }

    rr = requests.post(endpoint, json=msg,
                        headers={'Content-type': 'application/json',
                                'Authorization': token['token_type'] + ' ' + token['access_token']})

CLIENT_ID = os.getenv('CLIENT_ID')
CLIENT_SECRET = os.getenv('CLIENT_SECRET')
TENANT_ID = os.getenv('TENANT_ID')
USER_NAME = os.getenv('USER_NAME')
PASSWORD = os.getenv('PASSWORD')
TEAM_ID = os.getenv('TEAM_ID')
CHANNEL_ID = os.getenv('CHANNEL_ID')

img = cv2.imread('frame_000000.png')

token = get_token(CLIENT_ID, CLIENT_SECRET, TENANT_ID, USER_NAME, PASSWORD)
send_image(token, img, token, TEAM_ID, CHANNEL_ID)
  • Image

frame_000000.png

Microsoft Teams
Microsoft Teams
A Microsoft customizable chat-based workspace.
9,234 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,821 questions
Microsoft Teams Development
Microsoft Teams Development
Microsoft Teams: A Microsoft customizable chat-based workspace.Development: The process of researching, productizing, and refining new or existing technologies.
2,916 questions
{count} votes