Issues with Uploading Image to DeviantArt’s Sta.sh with Title, Description, and Tags

I am trying to upload an image to DeviantArt’s Sta.sh and publish it with a specific title, description, and tags using the DeviantArt API. However, while the title is being correctly applied, the description and tags are not being set. Here’s the code I’ve written and what I’ve tried so far:

Code:

import requests
import os
import webbrowser
import http.server
import socketserver
import threading
import urllib.parse

# Replace these with your actual credentials and URLs
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
REDIRECT_URI = 'http://localhost:8000/callback'
AUTH_URL = 'https://www.deviantart.com/oauth2/draft15/authorize'
TOKEN_URL = 'https://www.deviantart.com/oauth2/token'
UPLOAD_URL = 'https://www.deviantart.com/api/v1/oauth2/stash/submit'
PUBLISH_URL = 'https://www.deviantart.com/api/v1/oauth2/stash/publish'

# Global variables to store authorization code and server state
authorization_code = None
server_running = True

# Handler for the OAuth2 redirect
class OAuth2RedirectHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        global authorization_code, server_running
        self.send_response(200)
        self.end_headers()
        query = urllib.parse.urlparse(self.path).query
        params = urllib.parse.parse_qs(query)
        authorization_code = params.get('code', [None])[0]
        self.wfile.write(b"Authorization successful. You can close this window.")
        server_running = False

# Function to start the local server in a separate thread
def start_local_server():
    with socketserver.TCPServer(("localhost", 8000), OAuth2RedirectHandler) as httpd:
        print("Local server started. Waiting for authorization...")
        while server_running:
            httpd.handle_request()

# Step 1: Redirect user to DeviantArt for authorization
def get_authorization_code():
    params = {
        'response_type': 'code',
        'client_id': CLIENT_ID,
        'redirect_uri': REDIRECT_URI,
        'scope': 'stash publish'
    }
    auth_url = f"{AUTH_URL}?{urllib.parse.urlencode(params)}"
    print(f"Opening browser for authorization: {auth_url}")
    webbrowser.open(auth_url)

    # Start local server in a separate thread
    server_thread = threading.Thread(target=start_local_server)
    server_thread.start()

    # Wait for the server to receive the authorization code
    server_thread.join()

    return authorization_code

# Step 2: Exchange authorization code for access token
def get_access_token(auth_code):
    data = {
        'grant_type': 'authorization_code',
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'code': auth_code,
        'redirect_uri': REDIRECT_URI
    }
    response = requests.post(TOKEN_URL, data=data)
    response.raise_for_status()
    return response.json()['access_token']

# Step 3: Upload image to Sta.sh with title, description, and tags
def upload_image(file_path, access_token, title, description, tags):
    headers = {'Authorization': f'Bearer {access_token}'}
    files = {'file': open(file_path, 'rb')}
    data = {
        'title': title,
        'body': description,
        'tags': tags,
        'is_mature': 'false'
    }
    response = requests.post(UPLOAD_URL, headers=headers, files=files, data=data)
    response.raise_for_status()
    return response.json()['itemid']

# Step 4: Publish image from Sta.sh
def publish_image(access_token, itemid):
    data = {
        'access_token': access_token,
        'itemid': itemid,
        'terms_agree': 1,
        'is_mature': 'false'
    }
    response = requests.post(PUBLISH_URL, data=data)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError as e:
        print(f"Error publishing image: {e}")
        print(f"Response content: {response.content}")
        raise
    return response.json()

def main():
    auth_code = get_authorization_code()
    access_token = get_access_token(auth_code)

    image_path = 'YOUR_IMAGE_PATH'  # Update with your image path
    title = 'Your Title'
    description = 'Your Description'
    tags = 'tag1, tag2, tag3'

    # Step 1: Upload image
    itemid = upload_image(image_path, access_token, title, description, tags)
    print(f"Uploaded image with item ID: {itemid}")

    # Step 2: Publish image
    publish_response = publish_image(access_token, itemid)
    print(f"Published image: {publish_response}")

if __name__ == '__main__':
    main()

Issue:
Title is being correctly applied.
Description and Tags are not being set during the upload process.

Question: What am I missing, or what should I adjust to ensure that the description and tags are correctly included during the upload phase?

Debugging Print Statements: I added print statements to check the parameters being sent to the API.
Updating API Endpoints: Ensured that the UPLOAD_URL and PUBLISH_URL are correct as per the latest DeviantArt API documentation.
Parameter Placement: Double-checked the placement of title, description, and tags in the upload_image function.

New contributor

Castle Thief thief cake is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật