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.
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:
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.