B4J Question [SOLVED] Google Oauth2 And Refresh Token

Star-Dust

Expert
Licensed User
Longtime User
When I ask for the token following Erel's example (here) it works correctly and tells me that the token will last 60 minutes. When I refresh nothing changes, it gives me a response that doesn't change the duration and it doesn't give me a refresh token.

Following the instructions of several tutorials I set my application as public (no longer in testing phase) but nothing changed.

Other tutorials say that if it doesn't give you a Refresh Token, you should use the same Token for the refresh....

At the end of all experiments after 60 minutes I always have to authenticate again.

There is a solution?
 
Solution
it doesn't give me the refresh token back like you see in my logs.
You are not requesting OAuth in OFFLINE-Mode.
Probably that´s the problem.


You´ll only get a Refreshtoken in offline-mode.
Snap1.png


I recently had the same problem with my Dropbox-Library.
I had to rewrite the OAuthflow to use offline-mode to get a refreshtoken which i can use to generate a new AccessToken whenever i need to.

Star-Dust

Expert
Licensed User
Longtime User
Yes, I have to re-authenticate with the browser. I know you can, unfortunately I can't. I would like to understand why it doesn't give me any refresh-token code
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It becomes false without deleting the file and without resetting. After 60 minutes it no longer works. If I call refreshtoken 5 minutes before 60 minutes it returns "expired in 300" i.e. 5 minutes
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
I've tested them both, I've been studying them for 10 days. If I ask for a refresh before 60 minutes it gives me back the remaining time. If I call after 60 minutes it says the Token is not valid.


However, at the first login it should give me a refresh code in addition to the token, but it doesn't send it, some guides say that you have to use the same token as refresh, but it doesn't work
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
First connection

{
"access_token": "ya29.a0Ad...",
"expires_in": 3599,
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}

request renewal after 55 minutes
Getting access token from refresh token...
{
"access_token": "ya29.a0Ad52N3....",
"expires_in": 288,
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}

request after 60 minutes
Getting access token from refresh token...
ResponseError. Reason: , Response: {
"error": "invalid_grant",
"error_description": "Token has been expired or revoked."
}
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
it doesn't give me the refresh token back like you see in my logs.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
it doesn't give me the refresh token back like you see in my logs.
You are not requesting OAuth in OFFLINE-Mode.
Probably that´s the problem.


You´ll only get a Refreshtoken in offline-mode.
Snap1.png


I recently had the same problem with my Dropbox-Library.
I had to rewrite the OAuthflow to use offline-mode to get a refreshtoken which i can use to generate a new AccessToken whenever i need to.
 
Last edited:
Upvote 2
Solution

Star-Dust

Expert
Licensed User
Longtime User
You are not requesting OAuth in OFFLINE-Mode.
Probably that´s the problem.


You´ll only get a Refreshtoken in offline-mode.
View attachment 151911

I recently had the same problem with my Dropbox-Library.
I had to rewrite the OAuthflow to use offline-mode to get a refreshtoken which i can use to generate a new AccessToken whenever i need to.
Resolved. That was exactly the problem. Thank you so much
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
B4X:
Private Sub Authenticate
#if B4J
    PrepareServer
#End If
    Dim link As String = BuildLink("https://accounts.google.com/o/oauth2/v2/auth", _
         CreateMap("client_id": mClientId, _
        "redirect_uri": GetRedirectUri, _
        "response_type": "code", "scope": mScope, "access_type":"offline"))
#if B4A
    Dim pi As PhoneIntents
    StartActivity(pi.OpenBrowser(link))
#else if B4i
    Main.App.OpenURL(link)
#else if B4J
    fx.ShowExternalDocument(link)
#end if
End Sub
 
Upvote 0
Top