@@ -47,52 +47,71 @@ codeunit 50126 "API Web Service"
47
47
RequestMessage: HttpRequestMessage ;
48
48
RequestContent: HttpContent ;
49
49
TokenOutStream: OutStream ;
50
+ Cert: Record "Isolated Certificate";
51
+ ClientAssertion: Text ;
52
+ HttpAuthUtils: Codeunit "HttpAuthUtils";
50
53
begin
51
54
// Create webservice call
52
55
RequestMessage. Method := ' POST' ;
56
+
57
+ if ApiSetup. Audiance = ' ' then
58
+ Error( ' Please set the audiance parameter in the EAPI Setup Page. Ex: https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token' ) ;
59
+
53
60
RequestMessage. SetRequestUri( ApiSetup. Audiance) ;
54
61
55
62
// Create webservice header
56
63
RequestMessage. GetHeaders( RequestHeader) ;
57
64
58
- // Payload needed? This might as well be a different implementation!
59
- // It's just an example where the credentials are stored as a json payload
65
+ if ApiSetup. "Client Id" = ' ' then
66
+ Error( ' Please set the Client Id parameter in the EAPI Setup Page' ) ;
67
+
68
+ if ( ApiSetup. "Client Secret" = ' ' ) and ( ApiSetup. "Client Certificate" = ' ' ) then
69
+ Error( ' Please set the Client Secret or Client Certificate parameter in the EAPI Setup Page' ) ;
70
+
71
+ if ( ApiSetup. Scope = ' ' ) then
72
+ Error( ' Please set the Scope parameter in the EAPI Setup Page' ) ;
73
+
74
+ if ApiSetup. "Authentication Method" = ApiSetup. "Authentication Method"::"Client Secret" then begin
75
+ AuthPayload := ' grant_type=client_credentials&client_id=' + ApiSetup. "Client Id" + ' &client_secret=' + ApiSetup. "Client Secret" + ' &scope=' + ApiSetup. "Scope";
76
+ end
77
+ else begin
78
+ Cert := HttpAuthUtils. GetCertificate( ApiSetup. "Client Certificate", ApiSetup. "Client Certificate Password") ;
60
79
61
- // Create json payload
62
- JObjectRequest . Add ( ' client_id ' , ApiSetup . "Client Id" ) ;
63
- JObjectRequest . Add ( ' client_secret ' , ApiSetup. "Client Secret" ) ;
64
- JObjectRequest . WriteTo ( AuthPayload ) ;
80
+ ClientAssertion := HttpAuthUtils . ComputeClientAssertion ( ApiSetup, Cert ) ;
81
+
82
+ AuthPayload := ' grant_type=client_credentials&client_id= ' + ApiSetup. "Client Id" + ' &Scope= ' + ApiSetup . Scope + ' &client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer&client_assertion= ' + ClientAssertion
83
+ end ;
65
84
66
- // Get Request Content
67
85
RequestContent. WriteFrom( AuthPayload) ;
68
86
69
87
RequestContent. GetHeaders( RequestHeader) ;
88
+ RequestHeader. Add ( ' charset' , ' UTF-8' ) ;
70
89
RequestHeader. Remove( ' Content-Type' ) ;
71
- RequestHeader. Add ( ' Content-Type' , ' application/json ' ) ;
90
+ RequestHeader. Add ( ' Content-Type' , ' application/x-www-form-urlencoded ' ) ;
72
91
73
92
RequestMessage. Content := RequestContent;
74
93
75
94
// Send webservice query
76
95
WebClient. Send( RequestMessage, ResponseMessage) ;
77
96
78
- if ResponseMessage. IsSuccessStatusCode() then begin
79
- ResponseMessage. Content(). ReadAs( ResponseText) ;
97
+ ResponseMessage. Content(). ReadAs( ResponseText) ;
80
98
99
+ if ResponseMessage. IsSuccessStatusCode() then begin
81
100
if not JObjectResult. ReadFrom( ResponseText) then
82
- Error( ' Error Read JSON' ) ;
101
+ Error( ' API Web Service Error. Error Read JSON' ) ;
83
102
84
103
TokenResponseText := GetJsonToken( JObjectResult, ' access_token' ). AsValue(). AsText() ;
85
- TokenExpiry := GetJsonToken( JObjectResult, ' expiry_date ' ). AsValue(). AsDateTime () ;
104
+ TokenExpiry := CurrentDateTime () + GetJsonToken( JObjectResult, ' ext_expires_in ' ). AsValue(). AsInteger () ;
86
105
87
106
end else
88
- Error( ' Webservice Error' ) ;
107
+ Error( ' API Web Service Error. Reason: %1 Message: %2 ' , ResponseMessage . ReasonPhrase, ResponseText ) ;
89
108
90
109
exit ( TokenResponseText) ;
91
110
end ;
92
111
93
112
local procedure GetJsonToken( JsonObject : JsonObject ; TokenKey: Text ) JsonToken : JsonToken ;
94
113
begin
95
114
if not JsonObject . Get( TokenKey, JsonToken ) then
96
- Error( StrSubstNo( ' Token %1 not found' , TokenKey)) ;
115
+ Error( StrSubstNo( ' API Web Service Error. Token %1 not found' , TokenKey)) ;
97
116
end ;
98
117
}
0 commit comments