By Guest on Friday, 12 November 2021
Posted in General
Replies 9
Likes 0
Views 1.7K
Votes 0
Hi,
I need to access an API of an Italian public administration via OpenID Connect OAuth2 and in "client credentials grant" mode.
I got a "Client id" and a "Secret" string.

I need to request the JWT type access token by passing the following mandatory parameters:
tokenEndpoint = @ "https://identity.ecocerved.it/connect/token";
grant_type = "client_credentials";
client_id = [ClientId ]
client_secret = [Secret]
apiBaseUrl = "https://demovivifir.ecocamere.it/api"

I have no experience using sgcWebSockets and would be grateful for some suggestions:
1) which objects to use? (I thought TsgcHTTP2Client and sgcHTTP_OAuth2_Client)
2) how to pass the parameters and make the first JWT token request?
3) how to make subsequent requests using the JWT token?

I enclose the manual that was provided to me (in Italian).

I would be very grateful for your help.
Greetings.
Francesco Sgaravatti
Hello,

I don't see any manual attached but most probably you first must request a OAuth2 token and then pass this token as an HTTP Header.
You can use the HTTP/2 client + OAuth2 component. There is a simple example of how use HTTP/2 and OAuth2 with google services:

https://www.esegece.com/help/sgcWebSockets/#t=Components%2FHTTP%2FHTTP2%2FClient%2FAuthentication%2FHTTP_2_and_OAuth2.htm

In short, first create a TsgcOAuth2 client, fill the settings and assign to the HTTP/2 property "Authentication.Token.OAuth". Then when you do a request, example a GET, using the HTTP/2 client, the HTTP/2 client detects that has attached an OAuth2 component, so first request a new token and then pass the token as an HTTP Header, it's all handled internally, so you don't need to worry to pass the token obtained using OAuth2. Next requests, the client will use the token till expires, when the token expires, the OAuth2 component requests a new one.

Kind Regards,
Sergio
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Sergio,
I Thank a lot you for your help.
I tried to follow your example but now I get an Access Violation error $c00000005 (only in debug mode) followed by a "Cannot connect to server" message.
I looked at the project settings:
Project> Options> C ++ Linker "Link with Dynamic RTL"
Project> Options> Packages> Runtime Packages "Link with runtime packages"
and are set to "false".

I enclose the screen-shots.

I would appreciate any suggestions.
Thank you in advance.

Kind Regards
Francesco Sgaravatti
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello,

I don't think the authentication URL is correct: https://identity/ecocerved.it, this url doesn't works
Use the OAuth2 client which is already compiled to get first the Auth2 token, it will be easy to debug if the OAuth2 request token works or not.

Kind Regards,
Sergio
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Good morning,
I made a Delphi program to test the Access Violation problem using sgcHTTP2Client.
I skipped Auth2 authentication by manually copying the "Bearer token" from the web portal using my credentials.

1) Then I carried out a first test as follows:
sgcHTTP2Client1.Request.BearerAuthentication: = True;
sgcHTTP2Client1.Request.BearerToken: = 'eyJhbGciOiJSUzI1 ....
and I got the compile error: [dcc32 Error] Unit4.pas (59): E2056 String literals may have at most 255 elements

2) If instead I copy the "Bearer token" from the Memo:
sgcHTTP2Client1.Request.BearerToken: = Memo1.Lines.Text;
I got the exception class $c00000005 Access violation

I attached the screen shots of the tests carried out.
Best Regards
Francesco
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello,

The error E2056 String literals may have at most 255 elements is raised by the IDE, you cannot set a string with more than 255 characters per line, so you should split the token in several lines. example:

sgcHTTP2Client1.Request.BearerToken := 'lasjdflasjdflaskdjf.....' +
'asdfjlasdjfsladfjsadlf....' +
'asldjfalsdjfsladjflsdjfk...' +
'asdlfkjasldfjsdalfj....' +
....

The second error I cannot reproduce and I cannot see the full code because you only attached a screenshot. I've created a simple sample where you can set the bearer token and do the HTTP request. In my tests, the server always return a 404 error.

HTTP2_OAuth2

In your code you don't save the response from server, and there is a final line where you create a TStringStream and do a ShowMessage that it makes no sense for me. What are you trying to do?

I hope this helps.

Kind Regards,
Sergio
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello Sergio !
Thank you a lot for your sample project.
I corrected my project using your code e so i have attached that to verify the AV.

Kind Regards
Francesco
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello,

I don't get any exception, I get an error that token is not valid, download the compiled demo, update the token and test again.

https://www.esegece.com/download/samples/HTTP2_OAuth2_2.zip

Kind Regards,
Sergio
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hi,
Okay, your compiled demo works ! The AV problem remain compiling on my PC. Do I have install OpenSSL 1.0.2u ? (now is 1.0.0d)

Francesco
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
Hello,

No, the openssl are included in the demo I sent to you, just put the openssl libraries in the same folder where is the executable.

Kind regards,
Sergio
·
2 years ago
·
0 Likes
·
0 Votes
·
0 Comments
·
View Full Post