30 Nov Data Extraction from Wix API
The Wix Data API allows you to work with your database collections using code and gives you more capabilities than what is available in the Editor alone. You can use this API to work with your site’s registered users. This API lets you get information about the current user, prompt a user to log in, and can log out the current user. The Wix-users API can be used in your site’s front-end code.
Here is the list of the processes we would follow in this article to get data from the Wix API:
- Create an application under the Wix developer center
- Set up permission and derive the token, authorization, code, access token, and refresh token
- Get the data you need
Create an application under the Wix Developer center
Step 1: To get started with the API, we need to first go to the website https://dev.wix.com/ and create a new application via the developer center for our Wix account. Afterward, we would set our Redirect URL & App URL on the OAuth tab.
Step 2: The next step is to add new permission under the permissions tab, where we select the kind of permission we need from the different categories (Wix Stores, Pricing Plans, Marketing, Wix Events, Contacts & Members, etc.). Any of these permissions we set, actually indicates the type of data we are interested in.
Step 3: After this has been done, we will test our app. It will prompt us to install the app and redirect us to the App URL we defined. From here we will generate a token code under the URL params returned. We’ll then proceed to authenticate by using the token code we obtained alongside other credentials available.
For Authentication
In getting the authorization code and access token, we can use the Flask framework in form of a web application to generate the details we need.
We’ll use the authentication URL endpoint with the token code, appId, and Redirect URL as a connection string. When the flask app has been ran, it will generate our authorization code.
from flask
import
Flask,redirect,url_for
import
config
import
requests
app = Flask(__name__)
app.config.from_pyfile("config.py", silent=True)
import
json
@app.route(‘/callback’)
def oauth_callback():
redirect_url = ‘https://cndro.com/’
auth_url = (‘https://www.wix.com/installer/install?’ +
‘token=’ +app.config[“TOKEN_CODE”]+
‘&appId=’ +app.config[“APP_ID”]+
‘&redirectUrl=’ + redirect_url)
return redirect(auth_url)
Generate Access Token and Refresh Token
To generate the Access token and Refresh token, we’ll use the temporary authorization code returned, together with the URL endpoint and other credentials to request an access token and a refresh token.
@app.route('/access')
def wixAccessToken():
the_url = ' https://www.wix.com/oauth/access'
response = requests.post(the_url,
headers = {
'Content-Type': 'application/json'
},
data = {
'grant_type' : 'authorization_code',
"client_id": app.config["APP_ID"],
"client_secret": app.config["APP_SECRET"],
"code": app.config["AUTH_CODE"]
})
response_data = json.loads(response.text)
return response_data
We must note that this access token expires in 5 minutes, because of this we must generate a new access token from the refresh token obtained. We’ll use the same URL endpoint we used earlier and also indicate our grant type as ‘refresh_token’.
@app.route('/refresh')
def NewAccessToken():
the_url = ' https://www.wix.com/oauth/access'
response = requests.post(the_url,
headers = {
'Content-Type': 'application/json'
},
data = {
'grant_type' : 'refresh_token',
"client_id": app.config["APP_ID"],
"client_secret": app.config["APP_SECRET"],
"refresh_token": app.config["REFRESH_TOKEN"]
})
response_data = json.loads(response.text)
return response_data
if __name__ == '__main__':
app.run(host='localhost',port=5000)
We can get data from any of the permissions categories we added by using its URL endpoint and with our Access token.
You can as well use other HTTP Methods apart from GET such as POST, GET, PATCH, DELETE with any of the endpoints you’re interested in working with.
An example code to get data from the Contact & Members Endpoint
def getListContact():
the_url = '
https://www.wixapis.com/contacts/v4/contacts
? paging.limit=1000&paging.offset=1000'
response = requests.get(the_url,
headers = {
'Authorization': wixAccessToken()
})
response_data = json.loads(response.text)
return response_data
print(getListContact())
No Comments