pythonmicrosoft-graph-apimicrosoft-graph-mail

Microsoft Graph API move junk email into Inbox not working (giving 400 response)


For ref I am sharing code here. For the documentaiton I am following this link https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http

but it is giving 400 error response enter image description here

def move_junk_to_inbox(message_id: str):
    status, access_token = get_token()

    headers = {
        "Content-Type": "application/json",
        "Authorization": access_token
        # "Prefer": 'outlook.body-content-type="text"',
    }
    folder_name = "Inbox"
    base_url = "https://graph.microsoft.com/v1.0/"
    end_point = f"users/{email_id}/messages/{message_id}/move"
    move_url = base_url + end_point

    body = {"destinationId": folder_name}

    response = requests.post(move_url, headers=headers, data=body, timeout=TIMEOUT)

    return response


Solution

  • I was able to solve it by using move event using this doc https://learn.microsoft.com/en-us/graph/api/message-move?view=graph-rest-1.0&tabs=http

    def move_email(self, email_id: str, message_id: str, target_folder: str):
            try:
                access_token, status = self.get_token()
                if not status:
                    raise "Failed to generate auth token"
    
                headers = {
                    "Content-Type": "application/json",
                    "Authorization": access_token,
                    "Prefer": 'outlook.body-content-type="text"',
                }
    
                body = {"destinationId": target_folder}
    
                end_point = f"{self.base_url}/users/{email_id}/messages/{message_id}/move"
                res = requests.post(end_point, headers=headers, json=body)
                return res.json()
    
            except Exception as e:
                err = f"Error move mail: {str(e)}"
                return None