Hello, I sent this query to upload a shopify hosted image to a certain product. The query runs fine, however on the web interface it gives this error message “Media upload failed Image: Media processing failed” and no image was uploaded.
Has anyone got the same issue?
# Define your Shopify API URL and access token
url <- "https://xxxx.myshopify.com/admin/api/2024-10/graphql.json"
access_token <- "xxx" # Replace with your actual access token
# Set up the headers for authorization and content type
headers <- add_headers(
`Content-Type` = "application/json",
`X-Shopify-Access-Token` = access_token
)
# Define the GraphQL mutation and variables for the media creation
query <- "
mutation productCreateMedia($media: [CreateMediaInput!]!, $productId: ID!) {
productCreateMedia(media: $media, productId: $productId) {
media {
alt
mediaContentType
status
}
mediaUserErrors {
field
message
}
product {
id
title
}
}
}
"
# Define the variables
variables <- list(
media = list(
list(
alt = "",
mediaContentType = "IMAGE",
originalSource = url
)
),
productId = paste0("gid://shopify/Product/", product_Id)
)
# Combine the query and variables into the JSON payload
payload <- toJSON(list(query = query, variables = variables), auto_unbox = TRUE)
# Send the POST request to Shopify's GraphQL API
response <- POST(url, headers, body = payload)
# Check the response status and parse the content
if (status_code(response) == 200) {
content <- content(response, "parsed")
print("Media creation response:")
print(content)
} else {
print(paste("Request failed with status code", status_code(response)))
print(content(response, "text"))
}
}
for(i in 1:length(inventory)){
iP <- inventory[[i]][['id']]
title <-inventory[[i]][['title']]
numberOfImages <- shop$getProductImage(productId = iP, imageId = '58801273635063')
FolderName <-FolderList[grep(paste0(title, '-'), FolderList)]
if(length(numberOfImages) ==0 &&
dir.exists(paste0(getwd(), '/',FolderName, '/edited_pics'))==TRUE){
#edited_pics
EditedPics = list.files(paste0(getwd(), '/',FolderName, '/edited_pics'), pattern=NULL,
all.files=FALSE, full.names=TRUE)
if(length(EditedPics)>0){
for(j in 1:length(EditedPics)){
img <- readJPEG(EditedPics[j])
if(dim(img)[1]/dim(img)[2] >=2/3 && dim(img)[1]/dim(img)[2] <=1.5){
product_id <- iP
ImageURL = UploadImage(imgPath = EditedPics[j])
AssignImage(product_Id = iP, url = ImageURL)
}
}
}
}```