Customer File Metafield upload through API

jacopol
Tourist
18 0 2

Hi! 
I'm having some issues with a customer file (and list of files) metafield to upload through the API. 

When an order is placed, I need to upload an xml file to the customer metafield that I previously created through the Shopify Admin. The metafield is of the type list of files, with file type generic. 

I managed to upload the file correctly, and I indeed see it in the Shopify Admin/Content/Files section, but I can't figure out how to correctly attach it to the metafield.

I tried 2 different approaches: 

1. Python API 
I run a python script that, after correctly handling the session and file upload, does this: 

    customer = shopify.Customer.find(ids = customer_id)[0]
    
    metafield = shopify.Metafield({
        'namespace': 'custom',
        'key': 'license',
        'value': data['resource_url'],
        'owner_id': customer_id,
        'type': 'file_reference'
    })

    response = customer.add_metafield(metafield)

where the data['resource_url'] is the staged upload file location. 

This simply returns metafield(None). If I try to slightly edit the metafield to add a one line text customer metafield, everything works as expected. 

2. REST API 
I request a POST with the following code: 

    headers = {
        'X-Shopify-Access-Token': access_token,
        'Content-Type': 'application/xml'
    }

    json = {
        'metafield': {
            'namespace': 'custom',
            'key': 'license',
            'value': data['resource_url'],
            'type': 'file_reference',
            'owner_id': customer_id
        },
    }

    
    response = requests.post('https://eftilo.myshopify.com/admin/api/'+str(api_version)+'/customers/'+str(customer_id)+'/metafields.json', headers=headers, json=json)


this simply returns a 400 Bad Access error. 

What am I doing wrong? 

Thanks!

Ciao!
Replies 2 (2)

ArchitechproOU
Shopify Partner
98 2 15

Did you include necessary scopes (write_customers) for your app according to https://shopify.dev/docs/api/usage/access-scopes#authenticated-access-scopes?

jacopol
Tourist
18 0 2

Hi! I managed to fix this some time ago. Don't remember how, but it now works! 

Thanks anyway! 

Ciao!