How to use OAuth to authenticate Wix App within website

Hi everyone, I’m having trouble creating a simple app to run in my website because it seems I need to use OAuth but somehow can’t seem to be able to make it run. The details are below.

My website will have a heavy use of collections, so I decided to create a custom backup system to have more than the weekly automatic backup. My goal is to have one or maybe two backups per day.

I stumbled upon the Wix Backup API so I made use of it to manualy trigger backups. I have not been able to run the scheduled backups, but I did succeed in triggering a backup manually with a custom action (visiting the home page, in my example).

For this, I had to create a Test Token (in the “Try It Out” button in the above page), and that served its purpose. However, I believe that because it is a test token, it should have a short expiration date, so it stops working after a short period of time.

The other option that shows in the “Try It Out” button is to provide an Access Token, which seems to be received when setting up OAuth, which is used by Wix Apps.

So I went to the Apps section and created a new App. I’m using Wix Editor, but it seems the Apps can only be developed in Wix Studio Editor, so I did it there. I stored the code there, released the app, and installed it in my website (which is something kind of weird for me, because I don’t need any GUI - is it possible to make an App without GUI?)

Still, when using the Create Access Token API to generate a new token, I keep getting errors in my Logs, which say the following:

“OAuth token request failed: 400 - {“message”:“Field validation failed”,“details”:{“validationError”:{“fieldViolations”:[{“field”:“getBy.app”,“description”:“is invalid”}]}}}”

If I’m saying something wrong please correct me… There is definitely some problem in my workflow, but I have tried so many things that I got kind of confused by now… It should be fairly simple…

If anyone knows how to solve this, please do point me in the right direction!

Thanks in advance!

1 Like

Does anyone have any hints on this topic?

Right now I’m studying the possibility of using an Embedded Script + Automations… First time using these, not sure if it does what I need.

1 Like

I think there may be a bit of confusion here - hopefully I can clear some of it up :slight_smile:


You can write code on your site without the need to create apps or use OAuth. This article explains a little more about enabling dev mode on your site, and getting oriented - Getting Oriented

If you’re simply running manual backups of collections (rather than running it at specific points), you can do so via the dashboard UI - CMS: Restoring Collections with Backups | Help Center | Wix.com

OAuth is only needed when writing code externally from Wix. If you’re writing code within the editor, it already understands who is running the code, and doesn’t need authenticating in the same way.

It sounds like you need the following:

  • A Scheduled Job that will run at the desired interval
  • A webMethod that will be called by the Scheduled Job that runs the backup
3 Likes

@noahlovell

Thanks for the reply!

I’m sorry, I may have overcomplicated my question :sob:

I’m well aware that it is possible to write code in my site without creating apps (in fact, my site relies in a lot of code I added). However, regarding the specific case of periodic automatic collection backups, I did not find a way to do so (since the dashboard only provides a weekly option).

My goal is to create two automatic backups per day (every 12 hours). That’s it.

The Create Backup method in the API has this specified:

image

What would you advise me to do in this case?

The Scheduled Job does seem to work as well, but don’t I need to fulfill this authentication?

1 Like

If the OAuth flow seems complicated, try using an Admin API Key with access to Wix Data.

Note that this one key can work for ALL the sites in your account, and since database backup is an admin-level operation, this will be easy to work with as compared to the lengthy OAuth flow.

But if you are planning to run the code for the Scheduled Job from the backend of that very site, make sure to securely store your Admin API Key in the Secrets Manager. Else host this cron job on a different platform altogether, outside of Wix.

3 Likes

@noahlovell @Pratham

Apologies for the late reply, got a lot of issues in the meanwhile.

Thank you both for your help!
After searching about the Scheduled Jobs and the Admin API Key, I got my problem solved in a really simple way! I’m making good use of the Secrets Manager as well to save this and other keys I’m currently using (great tool!)

Once again, thank you so much for your help!!! :blush:

(Just as a side note, it would be cool and more straightforward if it was possible to choose the collections backup frequency! Every X days/hours, etc. I know it should be a low priority feature, and if it is not implemented, it should have its reasons. Just wanted to leave my comment on this :slightly_smiling_face: )

2 Likes