[h]The process basically includes to first get an access token and once you have this token, then use it with any further api call.[/h]
Usually you will find details about how to get the oauth2 access-token in the REST API documentation of the webservice which you want to use.
Code could look something like this:
[code] Dim dic As New Xojo.Core.Dictionary
dic.Value(“grant_type”) = “password”
dic.Value(“username”) = “myusername”
dic.Value(“password”) = “mypassword”
dic.Value(“client_id”) = “AHxyKGssNdRz6bBrvqS3iREARka5FaTJ3kXRDFSS”
dic.Value(“client_secret”) = “QysGoB8jIMhCsCr1zTOKq1THhbTBohAyjD2PNg1i”
Dim json As Text
Try
json = Xojo.Data.GenerateJSON(dic)
Catch
Dim jError As xojo.Data.InvalidJSONException
MsgBox jError.Message + EndOfLine + EndOfLine + jError.Reason
Break
Return
End Try
// Convert Text to Memoryblock
Dim data As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.UTF8.ConvertTextToData(json)
// Assign to the Request’s Content
Self.sock.SetRequestContent(data,“application/json”)
// Set the URL
Dim url As Text = “https://myserver.com/oauth/token/”
// Send Asynchronous Request
Self.sock.Send(“POST”,url)[/code]
Then, in the PageReceived handler you may have code similar to this:
[code]If HTTPStatus = 200 Then
// TODO: check: required square brackets
buffer = “[” + buffer +"]"
Try
records = Xojo.Data.ParseJSON(buffer)
Dim rec As Xojo.Core.Dictionary = records(0)
If rec.Value("token_type") = "bearer" Then
// save new access token in property
Self.access_token = rec.Value("access_token")
// Now go an make more API calls
GoUseWebService
End If
Catch
Dim jError As xojo.Data.InvalidJSONException
MsgBox jError.Message + EndOfLine + EndOfLine + jError.Reason
Break
End Try
Else
MsgBox("Status: " + Str(HTTPStatus) + EndOfLine + EndOfLine + Content.ToString)
End If[/code]
Once we have the access-token, then we use it to authenticate subsequent api calls, for instance we may have a method 'GoUseWebService ’ with code like this:
[code] Self.sock.RequestHeader(“Authorization”)= "Bearer " + Self.access_token
// Set the URL with pagination parameter
Dim url As Text = “https://myserver.com/wp-json/wp/v2/media/?per_page=100”
// Example for 5 pictures per page, show page 2 :
’ Dim url As Text = “https://myserver.com/wp-json/wp/v2/media/?per_page=5&page=2”
// Send Asynchronous Request
Self.sock.Send(“GET”,url)[/code]