Moving content from discord channel to Teams Channel

Richard 20 Reputation points
2024-01-12T10:41:17.1266667+00:00

I have the task of moving content from Discord channels to Teams channels. I have exported the content from discord successfully. I have then tried to follow the instructions https://learn.microsoft.com/en-us/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams to import the messages into Teams. The code is below. I am getting insufficient permissions.

I am assuming there is a scope issue, as I believe I would need

scopes = ["User.Read", "ChannelMessage.Send"]

but to do so I would then need the user to log in. I tried to implement this in the second code snip below but I don't know what key is right to use for the device_flow and It is not practical for every user to log in so that their messages can be copied across.

This is really frustrating as the instruction page linked above makes it seem so easy. I would appreciate any help that the community can give me in solving this issue.

#============ start of API setup ================


client_id = "79...4f"
client_secret = "72...j0"
tenant_id = "28...21"
team_id = "67...cf"
channel_id = "19:15...df%40thread.tacv2"


endpointpermissions_url = f"https://graph.microsoft.com/{tenant_id}/oauth2/v2.0/token"


confidential_client = ConfidentialClientApplication(    
    client_id,    
    authority=f"https://login.microsoftonline.com/{tenant_id}",      
    client_credential=client_secret)

token_response = confidential_client.acquire_token_for_client(scopes=scopes)

headers = {"Authorization": "Bearer " + token_response['access_token'],    
           "Content-Type": "application/json",}



# process the file from discord in loop

    graph_api_body = {
        "createdDateTime": created_date_time,
        "from": {
            "user": {
                "id": user_info['id'],
                "displayName": user_info['displayName'],
                "userIdentityType": "aadUser"
            }
        },
        "body": {
            "contentType": "html",
            "content": f"<div>{text_content}</div>"
        }
    }

    response = requests.post(graph_api_url, headers=headers, json=graph_api_body)




scopes = ["User.Read", "ChannelMessage.Send"]

# Create a Confidential Client Application instance
confidential_client = ConfidentialClientApplication(
    client_id,
    authority=f"https://login.microsoftonline.com/{tenant_id}",
    client_credential=client_secret
)

# Get device code and user code
device_flow = confidential_client.initiate_auth_code_flow(scopes=scopes)
print(device_flow['message']) ### WHAT DO I USE HERE?
print("Open the URL: %s" % device_flow['verification_uri'])
print("Enter the code: %s" % device_flow['user_code'])

# Poll for the token using the device code
token_response = None
while token_response is None:
    time.sleep(device_flow['interval'])
    token_response = confidential_client.acquire_token_by_device_code(device_flow)

Microsoft Teams | Development
Microsoft Teams | Microsoft Teams for business | Other
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.