Android Question Modify Google OAuth2 to Spotify OAuth 2 - Help!

GJREDITOR

Member
I am trying to modify the Google OAuth2 Class from this Thread to Spotify OAuth 2 . I am able to get the "code" after the redirect URI login page shows up, but I am not able to get the token. The error message is :

ResponseError. Reason: Bad Request, Response: {"error":"invalid_client"}
Token reset!!!
Error accessing account.
I have modified the following in B4J code. The python code from web: Spotify Python Code might help understand the changes which were made:
scope changed in B4Xpage Created:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    xui.SetDataFolder("B4XGoogleContacts") 'only required for B4J
    oauth2.Initialize(Me, "oauth2", ClientId, "user-read-private user-read-email", _
        ClientSecret, xui.DefaultFolder)
    'oauth2.ResetToken 'uncomment if you changed the scope or want to reset the token
End Sub
Url changed to Spotify authorization url:
Download url changed:
Sub btnGetData_Click
    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        Log("Error accessing account.")
        Return
    End If
    Dim j As HttpJob
    j.Initialize("", Me)
   
    j.Download("https://api.spotify.com/v1/me")
    j.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        ParsePersonData(j.GetString)
    Else
        oauth2.ResetToken
        Log("Online data not available.")
    End If
    j.Release
End Sub
In the class, following changes were made:
url changed:
rivate Sub Authenticate
#if B4J
    PrepareServer
#End If
    Dim link As String = BuildLink("https://accounts.spotify.com/authorize", _
         CreateMap("client_id": mClientId, _
        "redirect_uri": GetRedirectUri, _
        "response_type": "code", "scope": mScope))
#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

Post String was changed:
B4X:
Private Sub GetTokenFromAuthorizationCode (Code As String)
    Log("Getting access token from authorization code...")
    Dim j As HttpJob
    j.Initialize("", Me)
    Dim postString As String = $"grant_type=authorization_code&code=${Code}&redirect_uri=${GetRedirectUri}&"$
        postString = AddClientSecret(postString)
    Log($"${postString}"$)
    j.PostString("https://accounts.spotify.com/api/token", postString)
        
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        TokenInformationFromResponse(j.GetString)
    Else
        ResetToken
        RaiseEvent(False)
    End If
    j.Release
End Sub
Redirect URI in Spotify is set correctly. Client ID and ClientSecret are correct. Please let me know what could be the problem based on error message. Full Log is here :
Waiting for debugger to connect...
Program started.
Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Getting access token from authorization code...
encoded credentials = MTkwYWM3ZjFlZTRmNDJmMWI4MzczYzAzYzAwYWRkNzA6NjM2NjlkZTAyOTAwNDVhYjg1NzVhN2FmN2Y0ODc2OTc=
s is grant_type=authorization_code&code=AQCRzluIWUOD3XlmqzKl23ucrQK7Jy4PiFawP8gVBCHW9luwhyxOsRoJoBKu9CTlUkierd8mFq48gMOy16YhwcsHoyjjl2StPBkWY3i9yM9Z2nKqOheje5tNWBpWrgWmWcdAqASsviWxr-x4HANREwALC-ZNVNc9uKmRaYvt2B4As-LBEEIHlCAwqcqz&redirect_uri=http://127.0.0.1:51067&Authorization= Basic MTkwYWM3ZjFlZTRmNDJmMWI4MzczYzAzYzAwYWRkNzA6NjM2NjlkZTAyOTAwNDVhYjg1NzVhN2FmN2Y0ODc2OTc=
grant_type=authorization_code&code=AQCRzluIWUOD3XlmqzKl23ucrQK7Jy4PiFawP8gVBCHW9luwhyxOsRoJoBKu9CTlUkierd8mFq48gMOy16YhwcsHoyjjl2StPBkWY3i9yM9Z2nKqOheje5tNWBpWrgWmWcdAqASsviWxr-x4HANREwALC-ZNVNc9uKmRaYvt2B4As-LBEEIHlCAwqcqz&redirect_uri=http://127.0.0.1:51067&Authorization= Basic MTkwYWM3ZjFlZTRmNDJmMWI4MzczYzAzYzAwYWRkNzA6NjM2NjlkZTAyOTAwNDVhYjg1NzVhN2FmN2Y0ODc2OTc=
ResponseError. Reason: Bad Request, Response: {"error":"invalid_client"}
Token reset!!!
Error accessing account.
Thanks in advance for all help,
 
Top