Skip to main content
5 of 6
added 12 characters in body

Error calling Stack Overflow Teams API using Python requests library

I'm trying to call a PUT/DELETE request using the Stack Overflow Teams API in Python, but I'm encountering errors. As an FYI, I have a working PAT that allows me to make get requests for articles and questions. Specifically, I'm trying to create an article, update an article, and delete an article using the API, but the PUT/DELETE requests don't seem to be working. When I try to run the code, I get an error message that says "no method found with this name" or {"error_id":400,"error_message":"title","error_name":"bad_parameter"}. The title might be the issue, but I don't think it goes against the requirements (string, 5 character+ length)

I have tried running the API calls using Python's requests module, and I have checked that my authentication token is correct. I have also tried adjusting the order of the payload and double-checking that the headers are correct. However, I'm still getting errors.

Here's my code (sanitized for privacy):

import json
import os
import requests
from markdown_it import MarkdownIt

def create_article(title, access_token):
    headers = {
        "X-API-Access-Token": access_token,
        "Content-Type": "application/json"
    }
    data = {
        "title": title,
        "body": "TEST",
        "tags": "REDACTED",
        "article_type": "knowledge-article",
        "filter": "default" 
    }
    json_data = json.dumps(data)
    api_url = "https://api.stackoverflowteams.com/2.3/articles/add?team=[TEAM_NAME]"
    response = requests.post(api_url, headers=headers, data=json_data)
    print(f"API response status: {response.status_code}")
    print(f"API response text: {response.text}")
    
    return response.json()


def delete_article(access_token, artId):
    headers = {
        "X-API-Access-Token": access_token,
        "Content-Type": "application/json"
    }
    data = {
        "id": artId
    }
    json_data = json.dumps(data)
    api_url = f"https://api.stackoverflowteams.com/2.3/articles/{artId}/delete?team=[TEAM_NAME]"
    response = requests.delete(api_url, headers=headers, data=json_data)
    print(f"API response status: {response.status_code}")
    print(f"API response text: {response.text}")
    
    return response.json()

def import_single_wiki_to_teams(input_directory, access_token, test_file):
    input_file = os.path.join(input_directory, test_file)

    if os.path.isfile(input_file):
        with open(input_file, "r", encoding="utf-8") as f:
            content = f.read()

        html_content = convert_md_to_html(content)
    #   title = os.path.splitext(test_file)[0]
        title = "TeArticle"
        print(f"Title from OS is: {title!r}")

        response = create_article('Test Article', access_token)  # Use the title and html_content variables
        print(response)
    else:
        print(f"File not found: {input_file}")


input_directory = PATH
access_token = [REDACTED]
test_file = "FILE_NAME"
import_single_wiki_to_teams(input_directory, access_token, test_file)