Search code examples
androidapiaccess-tokenonenote

Android- One Note Access Token refresh


OneNote provide an API to add a note to OneNote. To do this i need to do authentication to get user access token and use this token to add new note and other thing. the authentication is done by making the user enter his Microsoft account and password.

The problem is the access token expired after one hour so the user will properly have to authenticate every time he enter the app and i don't want that.

I have read the OneNote API guide and i found they say this:

The scopes needed by the OneNote API are at a minimum office.onenote_create. Tokens you receive using that scope will only be valid for one hour, so we recommend you also request the wl.offline_access scope. If the user grants your app access, the token can be refreshed for up to a year, or until the user revokes their permission. You can find more in-depth information in the Live Connect Developer Center Scopes and permissions documentation.

Here is the link

I understand that u can make the access token refresh and i have added the "wl.offline_access" in the scope but still i don't know how to make the access token refresh.


Solution

  • When you request the wl.offline_access, you will also get back a refresh token. Your access token still expires every hour, but you can use your refresh token (which is good for 1 year), to exchange for a new access token. This is a standard OAuth 2.0 pattern.

    For documentation on how to refresh your access token, see the documentation here: : http://msdn.microsoft.com/en-us/library/dn631818.aspx

    Search for "refresh" on that page. For your convenience, I am pasting a snippet of the documentation here:

    POST https://login.live.com/oauth20_token.srf    
    Content-type: application/x-www-form-urlencoded
    
    client_id=CLIENT_ID&client_secret=CLIENT_SECRET&redirect_uri=REDIRECT_URI&grant_type=refresh_token&refresh_token=REFRESH_TOKEN
    

    Also, if you look at the OneNote Developer samples on GitHub, you can also see how to do the token refresh on various platforms.

    Hope that helps.

    James (@jmslau)