Issues adding customers to a product


#1

I’m trying to get started with a new particle product using python.

Using the API docs and Guides my python script can do the following:

  • Get an access token using the client id and secret
  • Create a customer for a product using the access token
  • Create a customer for a product using OAuth client credentials grant type.

After creating customer accounts I try to get a list of all customer accounts associated with my product.

The problem is the API response suggests no customers have been added to the product even though the previous API calls to create accounts returned the appropriate responses.

My question is what am I missing here? Seems like the users accounts are getting created, but where? They aren’t showing up in our console and subsequent API calls can’t find them.

Here’s my commented python code illustrating the problem:

client_id = 'ustring-XXXX'
client_secret = 'xx37410220xx6x04x6x81x1x2d427141db0fc0b7'

import requests
from random import randint, seed
seed()

particle_url_root = 'https://api.particle.io'

# Get an access token using the client_id and client_secret
params = {
    'grant_type': 'client_credentials',
    'client_id': client_id,
    'client_secret': client_secret,
}
get_token_url = 'https://api.particle.io/oauth/token'
response = requests.post(get_token_url, params)
if 'access_token' not in response.json().keys():
    raise Exception('Access token not available.  Your client_id and client_secret are probably wrong.')
access_token = response.json()['access_token']

# create the access_token http header
access_token_header = {"Authorization": "Bearer " + access_token}

# Get the product
endpoint = '/v1/products'
response = requests.get(particle_url_root + endpoint,
                        headers=access_token_header)
product = response.json()['products'][0]

# create the user params
user_params = {
    'email': 'user+' + str(randint(0, 100000)) + '@gmail.com',
    'no_password': True
}

# Create a new user using the access_token_header and the /v1/products endpoint
new_customer_endpoint = '/v1/products/' + str(product['id']) + '/customers'
response = requests.post(particle_url_root + new_customer_endpoint,
                         user_params,
                         headers=access_token_header)
if not response.json()['ok']:
    raise Exception('User could not be created.  The email address has probably been used previously.')

# update the user params to test creating new users with the client_id and client_secret
user_params = {
    'client_id': client_id,
    'client_secret': client_secret,
    'email': 'user+' + str(randint(0,100000)) + '@gmail.com',
    'no_password': True
}

# Create a new user using the client_id and client_secret and the /v1/products endpoint
response = requests.post(particle_url_root + new_customer_endpoint,
                         user_params)
if 'scope' not in response.json():
    raise Exception('User could not be created.  The email address has probably been used previously.')

# Get all customers
endpoint = '/v1/products/' + str(product['id']) + '/customers'
response = requests.get(particle_url_root + endpoint,
                        headers={"Authorization": "Bearer " + access_token})
print 'List of customers for product: '
print response.json()['customers']

#2

@bryce


#3

@brigsy Did you find a solution to this? I’m also having the same problem, creating customers for a product, using two-legged auth. API comes back with access token etc. however no customer appears in the product console…

@jeiden any ideas?


#4

Ah I see this has been addressed here:

+1 @jeiden for a fix :slight_smile:

Also for general system development, having the ability to delete a customer from the console immediately after it is created would be very useful. I’m building a two-legged system where my server creates the customer and as I’m learning as I go, it would be really great to test making the same customer a few times while I fix my code!


#5

Hey @G65434_2,

Thanks for the feedback. I’ve added this to our internal list of things to improve on! Agreed that both of these suggestions could make for a better experience for product creators.

Jeff