Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30
Accepted Solution

OAuth2: Refreshing token before app starts

Hi guys!


I am in desperate need of help right now. I recently changed my app to use OAuth2, but it gives me an access token that i need to refresh every hour. The problem is that when my app starts, I need to make the API http call to refresh the token BEFORE any other API calls occur. I have tried so many different ways of doing this, but I can't get it quite right.

 

I tried using QEventLoop which is what I found online to be a way of making QNetworkManager request synchronous (since by nature it is asynchronous). First I tried doing this before creating the QML file...

 

 QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

 But then when I quit the QEventLoop, it wouldn't continue on with the rest of the code, I guess because the main event loop begins right after this line to creat the QML is called ^. So by entering a separate QEventThread before the main event thread has started, i can't seem to get back. 

 

So then I tried doing it right after creating the QML file via that line above. However, this also doesn't seem to work well because sometimes if the QNetworkReply takes a long time to come back, the QML files have already intiialized and started other API calls to retrieve data, without having a valid token to send so they all come back as failed http requests.

 

Does anybody know a good way of going about this? I need to make this Http request to refresh my authentication token BEFORE anything else happens, especially before any other API calls happen since otherwise they won't be valid..

 

Thank you!!

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: OAuth2: Refreshing token before app starts

easiest way would be before making any api calls to check the token if it's valid or must be refreshed

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: OAuth2: Refreshing token before app starts

The fastest way of doing it is to just refresh it, since checking whether its valid requires another separate API call and I want to reduce the startup time as much as possible.

My app uses a tabbedPane, each tab having at least one API call which all are asynchronous when done with QNetworkManager. For this reason I am unsure of how to synchronize the process of first refreshing the token and then making further API calls. The tricky thing is making sure that the refresh call occurs before any of the QML is loaded since that is when the other API calls occur.
Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: OAuth2: Refreshing token before app starts

aaah - understand

 

then at first you should change your Tabs and only do a lazy loading if they'll be touched the first time

https://developer.blackberry.com/native/documentation/cascades/ui/navigation/multiple_screens_tabs.h...

this will make sure the qml content won't be loaded before first TAP on the TAb

 

and from your first Tab you should check if you alreay got the token -

if not do your stuff to get the token while displaying a progress dialog or so

https://developer.blackberry.com/native/reference/cascades/bb__system__systemprogressdialog.html

 

then when you get the token do the first api call

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: OAuth2: Refreshing token before app starts

I did try using the Delegate class to load tabs as they are needed, but I ran into issues because at the root of each tab I have a NavigationPane. The delegate property asks for a source qml file which needs to have a Page element at its root. I could'nt figure out a way to maintain my navigationpanes while using the Delegate class to dynamically load my tabs. Do you know any way around this?

Developer
ekke
Posts: 1,571
Registered: ‎04-08-2010
My Device: Z10 (red Limited Edition), Q10, Z30
My Carrier: Telekom.de, O2, Vodafone

Re: OAuth2: Refreshing token before app starts


Paratheo wrote:

I did try using the Delegate class to load tabs as they are needed, but I ran into issues because at the root of each tab I have a NavigationPane. ....

-----

 

I'm doing the same without an issue. See here my Navigation Concept using TabbedPane as root and NavigationPanes on top of the Tabs:


Paratheo wrote:

.... The delegate property asks for a source qml file which needs to have a Page element at its root. I could'nt figure out a way to maintain my navigationpanes while using the Delegate class to dynamically load my tabs. Do you know any way around this?


You can do it using Delegates - see here some code. (scroll down to 'create dynamic and destroy')

 

 

 

-------------------------------------------------------------------------------
ekke (independent software architect, rosenheim, germany)

BlackBerry Elite Developer
BlackBerry Platinum Enterprise Partner
International Development Mobile Apps BlackBerry 10 Cascades
Cascades - Workshops / Trainings / Bootcamps

blog: http://ekkes-corner.org videos: http://www.youtube.com/user/ekkescorner http://vimeo.com/ekkescorner/videos
bb10-development: http://appbus.org Twitter: @ekkescorner
Developer
Paratheo
Posts: 134
Registered: ‎04-25-2009
My Device: Z30

Re: OAuth2: Refreshing token before app starts

Yay I got the delegate dynamic tab loading working now! Thank you!

However, the downside is that I can no longer support 10.1 :smileysad: