Hello there,
I've made an app that uses OAuth to retrieve user data, so I've used registerForOAuthMessages to define a callback function. In the codes hereafter (where the URLs have been obfuscated) I trigger the function "go" when a background event is fired, this function "go" makes the OAuth window pop-up
using Toybox.Communications as Comm; using Toybox.System as Sys; using Toybox.WatchUi as Ui; using Toybox.Application as App; using Toybox.Time.Gregorian as Calendar; const ClientId = "..."; const ClientSecret = "..."; const ApiUrl = "..."; const RedirectUri = "https://localhost/"; // The LoginTransaction is a special transaction that handles // getting the OAUTH token.$ (:background) class LoginTransaction { hidden var _delegate; // Constructor function initialize(delegate) { _delegate = delegate; Sys.println("initialize LoginTransaction"); Comm.registerForOAuthMessages(method(:accessCodeResult)); } // Handle converting the authorization code to the access token // @param value Content of JSON response function accessCodeResult(value) { Sys.println("accessCodeResult " + value); if( value.data != null) { Sys.println("accessCodeResult Ok"); } else { Sys.println("Error in accessCodeResult"); } } // Method to kick off tranaction function go() { // Kick of a request for the user's credentials. This will // cause a notification from Connect Mobile to file Sys.println("go"); Comm.makeOAuthRequest( // URL for the authorization URL "...",//there is a valid URL here but it is private // POST parameters {}, // Redirect URL $.RedirectUri, // Response type Comm.OAUTH_RESULT_TYPE_URL, // Value to look for {"responseCode" => "OAUTH_CODE", "responseError" => "OAUTH_ERROR"} ); } }
using Toybox.Background as Bg; using Toybox.System as Sys; // The Service Delegate is the main entry point for background processes // our onTemporalEvent() method will get run each time our periodic event // is triggered by the system. (:background) class JsonBackground extends Toybox.System.ServiceDelegate { hidden var _transaction=null; function initialize() { Sys.ServiceDelegate.initialize(); Bg.registerForTemporalEvent(new Time.Duration(5 * 60)); var ltd=new LoginTransactionDelegate(); _transaction = new LoginTransaction(ltd); } function onTemporalEvent() { System.println("onTemporalEvent2"); _transaction.go(); Bg.exit(1); } }
using Toybox.Background as Bg; using Toybox.System as Sys; using Toybox.Communications as Comm; using Toybox.WatchUi as Ui; (:background) class LoginTransactionDelegate extends TransactionDelegate{ // Constructor function initialize() { TransactionDelegate.initialize(); } // Handle a error from the server function handleResponse(data) { System.println("OK "+data); } function handleError(code) { var msg = "err"; msg += code; System.println(msg); }
The OAuth window pop-up correctly when the Background event is triggered, then I enter my credentials correctly and the window close but the "accessCodeResult" function seems not to be called.
Does someone have an idea?
Thanks in advance