MacroDroid and OAUTH examples

RSF

Well-known member
Can anyone point to a working example of using MacroDroid with a web service that requires OAUTH authentication (for example, Spotify, where one can start a playlist / album / track, and control players and volume, etc., via a web API, but only after authenticating via an OAUTH scheme)?

I have a working version in Tasker, using its "HTTP Auth" action (and standard HTTPS GET/PUT actions), invoking from MacroDroid via intents, so it's all working, but more complex than I'd like. I'd like to do it all in MacroDroid, but:
(a) can't find an analogous "Auth" action in MacroDroid,
(b) can't find examples of using MacroDroid in this type of scenario online generally, and
(c) can't find clear documentation or examples of doing an HTTPS OAUTH sequence using HTTPS GET/PUT/etc. actions

Thanks in advance for any tips / help.
 

MacroDroidDev

Administrator
Staff member
Sorry there is no OAUTH support directly in MacroDroid I'm afraid. I will add it to my TODO list to investigate when I have time.
 

RSF

Well-known member
Got some time to figure this out… The back-and-forth authorization between Spotify and MacroDroid is vanilla OAuth 2.0 -- you send some info to a Spotify webpage, it sends back an authorization code to a webpage, you send that back to another Spotify web URL with more info, it responds with an authorization token, and you then use that token in calls to do things like start playlists, control playback volume, etc…

The only MacroDroid-specific wrinkle is getting that initial authorization code into MacroDroid from the website to which Spotify sends the code. For this, I used MacroDroid's Webhook scheme, passing the code as a parameter on the MacroDroid webhook call. If you haven't used that feature in MacroDroid, you should read up on it and play with it for a while, to understand how one can use Webhooks to trigger (and send data to) MacroDroid macros by opening a MacroDroid web address.

Tthe attached Spotify OAuth token macro handles that whole Spotify OAuth scheme within MacroDroid and sets (and updates) an authorization token global variable, for use in calls to Spotify's API .

Steps

1) Register on Spotify developer site​

Register your new integration with Spotify online. This entails setting up a "developer" account at https://developer.spotify.com/dashboard with your existing Spotify user ID and password.

Create a new "app". Give it a name.

Enter the URL of the redirect URI web page. You're welcome to use the one I set up, which should work with Spotify and other services as long as the MacroDroid webhook scheme below is followed. Its address is
https://rpub109.s3.amazonaws.com/MD-OAuth-Redirect.html. Or you can take that page and host it yourself if you prefer.

Make note of the client ID, and client secret, that Spotify will give you.

2) Import and configure the Spotify token-management macro​

Back in MacroDroid, import the attached macro.

Update these local variables in the macro:
  • _client_id - the client ID for your app, provided by Spotify above
  • _client_secret - the client secret for your app, provided by Spotify
  • _desired_scopes - this will depend on how much you want to control Spotify. I used user-read-playback-state+user-modify-playback-state+user-read-currently-playing which covers most playback control operations. If you wanted to update playlists, you’d need to add other scopes (all separated by plus signs); see Spotify's API for details.
(The local variables that need to be configured have names beginning with an underscore; all other local variables in the macro are set by the actions in the macro as it executes)

(If you want to host your own redirect URI, update the redirect_URI local variable as well)

Save the macro. Re-open the macro and make sure the webhook trigger looks good – MacroDroid should automatically update it to your installation-specific webhook ID.

3) Trigger Spotify’s login/authorization flow​

The first action in the macro is an Open Website action. It’s disabled since you only run this once, manually. Tap the action to open MacroDroid’s action popup); then tap “Test action”.

4) Log in to Spotify​

Spotify should present a login page to you in your phone’s browser, in response. Log in to Spotify using your standard credentials–as if you were logging into its web music player.

Spotify should then show you the "scopes" you defined above and ask if you want to allow those scopes. Tap “OK” or “Yes”.

5) Send authorization into MacroDroid from redirect page​

Spotify should then send your authorization code to the redirect URI page, which will display in your browser. That page should display the information from Spotify (long strings of numbers and letters), and then after a moment will ask if you want to “Send to MD?” (MacroDroid). Tap “OK”.

The page then converts the data to a MacroDroid webhook call, and you should then see just a white page with “Ok” displayed (the standard MacroDroid webhook confirmation).

6) Macro asks for and gets authorization token​

Everything after that happens automatically in the macro. The MacroDroid macro should respond to the webhook, send the authorization code it receives back to Spotify, asking for an authorization token; it should then save the timeout and refresh token values it receives into local variables in the macro, as well as save the authorization token into a global variable (the authorization token is a global variable to allow access from other macros and action blocks).

7) Call Spotify to do things…​

Now that the authorization token exists in MacroDroid, you can issue HTTP Request MacroDroid actions to Spotify to control playback.

Macros and action blocks to control Spotify should look like so:
  • Add a Macro Run action as the first action, pointing to the imported macro, checking the "Block next actions until complete" action
  • Test the value of the global authorization token variable to ensure it's not empty after that call (if it is, something went wrong with the authorization flow, and you should stop this macro's execution)
  • Iinclude the token in the Header tab of all HTTP Request actions: Parameter name = “Authorization” (without quotation marks); value = {the global variable you defined for the authorization token}
For example, Spotify’s “Pause playback” command



[@MacroDroidDev - hope this info may help at some point. You'd probably be able to streamline a lot of this building w/in MacroDrolid]


Using this scheme with another service other than Spotify

To clone the macro with a different service (e.g. Sonos), change all references in the macro from "Spotify” to the service you're interested in:
  • the macro name
  • the MacroDroid Webhook trigger ID (the "webhook_trigger" local variable, plus the macro’s trigger)
  • the global variable holding the authorization token
  • the stopwatch ID (both in the macro’s stopwatch trigger, and the macro’s actions)
  • the get_code_URL variable to be the "Get Code" URL of that service
  • the get_token_URL variable to be the "Get Token" URL of that service
 

Attachments

  • Spotify-_Request_oauth_token.macro
    21.5 KB · Views: 24

RSF

Well-known member
One more point relative to using MacroDroid/OAuth with other services: check whether the service provides a way to generate a usable token online.

I was about to clone the scheme above for use with SmartThings, but found a SmartThings option for a "Personal Access Token", which one can simply request on their developer site. That eliminates the need for steps 1-6 above. You just copy and paste the token from the SmartThings site into a global variable in MacroDroid and use it in HTTP Request actions to control your home automation via their API.

Their Personal Access Tokens apparently expire in 50 years -- so there's no need to worry about expiration. You can always create a new one, with different scopes, at that same page.

Somewhat surprising ... seems a bit loose from a security standpoint for a system that could include home security devices like cameras, locks, and so forth. You do need to log into your Samsung account... And if you just have light bulbs and maybe a couple smart plugs, probably not that big a deal. Certainly simpler than the scheme above. Neither Spotify nor Sonos has a similar option -- Spotify has a Personal Access Token scheme but it expires quickly.
 
Hi.

I am not able to send mail in MD using Gmail account. It seems that Oauth is invoked but nothing happens. (See log and popup).

May someone that have used Oauth help me ?
 
Hi.

I am not able to send mail in MD using Gmail account. It seems that Oauth is invoked but nothing happens. (See log and popup).

May someone that have used Oauth help me ?
 

Attachments

  • Screenshot_20240327_183712_MacroDroid.jpg
    Screenshot_20240327_183712_MacroDroid.jpg
    252 KB · Views: 5
  • Screenshot_20240327_183649_One UI Home.jpg
    Screenshot_20240327_183649_One UI Home.jpg
    1.1 MB · Views: 5

RSF

Well-known member
You shouldn't need to use OAUTH to send an email from MacroDroid, as long as MacroDroid is the current Play Store version and you've authorized it as an authorized "Third-Party Service" on your Google account. To see if that latter aspect is in place,
  1. Go to myaccount.google.com/ in your browser
  2. Log in if needed
  3. Click Security in the left margin
  4. Scroll down to the "Your Connections to Third-Party Apps & Services" section
  5. Click on MacroDroid
  6. Ensure that "Send email on your behalf" is listed as a MacroDroid authorized feature
Do you have #5 and #6 set up? It should've happened automatically the 1st time MacroDroid tried to send an email from your account.
 
You shouldn't need to use OAUTH to send an email from MacroDroid, as long as MacroDroid is the current Play Store version and you've authorized it as an authorized "Third-Party Service" on your Google account. To see if that latter aspect is in place,
  1. Go to myaccount.google.com/ in your browser
  2. Log in if needed
  3. Click Security in the left margin
  4. Scroll down to the "Your Connections to Third-Party Apps & Services" section
  5. Click on MacroDroid
  6. Ensure that "Send email on your behalf" is listed as a MacroDroid authorized feature
Do you have #5 and #6 set up? It should've happened automatically the 1st time MacroDroid tried to send an email from your account.
Hi.

MacroDroid is not listed there.

Do I have to reinstall it ?

Thanks very much.
 

RSF

Well-known member
A good question; unfortunately, I don't know the answer. I think the scheme is normally that an app will request access to a user's Google account the first time it's needed, and after that, assuming a user granted access, everything's fine. I'm not sure how to force MacroDroid to re-request that access... Sorry I can't be more definitive; the "Send Email" action has always worked fine for me.

If you have only a few macros defined, then it might make sense to uninstall and re-install MacroDroid. Make sure you do a backup before uninstalling, so that you can recover them. But it really shouldn't be needed.

You might try, first, the "User Password Authentication" option in MacroDroid's Settings / EMail Settings screen:
1711595505336.png

If that doesn't work, I'd recommend starting a new thread in the MacroDroid forum, titled something like "Re-authorizing Google access" or "Lost my Google account access to send email; how to recover?"
 

MacroDroidDev

Administrator
Staff member
Please don't use the Password Authentication option as this rarely works nowadays that 2FA is strongly enforced.
 

MacroDroidDev

Administrator
Staff member
I am going to have a play around and see if I can get into a state where emails cannot be sent in this way.
 
A good question; unfortunately, I don't know the answer. I think the scheme is normally that an app will request access to a user's Google account the first time it's needed, and after that, assuming a user granted access, everything's fine. I'm not sure how to force MacroDroid to re-request that access... Sorry I can't be more definitive; the "Send Email" action has always worked fine for me.

If you have only a few macros defined, then it might make sense to uninstall and re-install MacroDroid. Make sure you do a backup before uninstalling, so that you can recover them. But it really shouldn't be needed.

You might try, first, the "User Password Authentication" option in MacroDroid's Settings / EMail Settings screen:
View attachment 9208

If that doesn't work, I'd recommend starting a new thread in the MacroDroid forum, titled something like "Re-authorizing Google access" or "Lost my Google account access to send email; how to recover?"
I reinstalled MD and it asked for permission about contacts, etc but nothing about email nor gmail.
 
Now MD appears at app list but only to make login with Google account.

Using SENDTO a macro generates an popup error.

Using INTENT a macro creates a new message but doesn't fill SUBJECT and TEXT and sticks.
 

Attachments

  • Screenshot_20240328_055519_Google Play services.jpg
    Screenshot_20240328_055519_Google Play services.jpg
    423.1 KB · Views: 6
  • Screenshot_20240328_054721_MacroDroid.jpg
    Screenshot_20240328_054721_MacroDroid.jpg
    1 MB · Views: 6
  • Screenshot_20240328_060347_MacroDroid.jpg
    Screenshot_20240328_060347_MacroDroid.jpg
    593.4 KB · Views: 7
Top