-
Notifications
You must be signed in to change notification settings - Fork 2
/
googleapinlpdemo.dpr
100 lines (92 loc) · 3.04 KB
/
googleapinlpdemo.dpr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
program googleapinlpdemo;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.DateUtils,
System.IOUtils,
System.Net.HTTPClient,
System.Net.HTTPClientComponent,
System.Net.URLClient,
System.Classes,
System.JSON,
System.Generics.Collections,
JOSE.Core.JWT,
JOSE.Core.JWK,
JOSE.Core.JWS,
JOSE.Core.JWA,
JOSE.Types.Bytes;
var
LToken: TJWT;
LDT: TDateTime;
LKey: TJWK;
LJWS: TJWS;
LCompactedToken: TJOSEBytes;
HTTPClient: TNetHTTPClient;
LStrStream: TStream;
LResponse: IHTTPResponse;
LJSONResponse: TJSONObject;
LHeaders: TNetHeaders;
LAccessToken: string;
LJSONReq, LObj, LFeat: TJSONObject;
LOutputStream: TStringStream;
LStreamReader: TStreamReader;
begin
try
// Create own JWT
LToken := TJWT.Create;
LToken.Claims.SetClaimOfType('iss',
'your_client_email'); //<------------ your client email goes here
LToken.Claims.SetClaimOfType('scope',
'https://www.googleapis.com/auth/cloud-language');
LToken.Claims.Audience := 'https://www.googleapis.com/oauth2/v4/token';
LToken.Claims.IssuedAt := Now;
LDT := Now;
IncMinute(LDT, 30);
LToken.Claims.Expiration := LDT;
LKey := TJWK.Create(TFile.ReadAllBytes('key.txt'));
LJWS := TJWS.Create(LToken);
LJWS.Sign(LKey, TJOSEAlgorithmId.RS256);
LCompactedToken := LJWS.CompactToken;
// Get access token
HTTPClient := TNetHTTPClient.Create(nil);
LStrStream := TStringStream.Create
('grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='
+ LCompactedToken.AsString);
SetLength(LHeaders, 1);
LHeaders[0].Name := 'Content-Type';
LHeaders[0].Value := 'application/x-www-form-urlencoded';
LResponse := HTTPClient.Post('https://www.googleapis.com/oauth2/v4/token',
LStrStream, nil, LHeaders);
LJSONResponse := TJSONObject.ParseJSONValue(LResponse.ContentAsString)
as TJSONObject;
LAccessToken := LJSONResponse.Values['access_token'].Value;
FreeAndNil(HTTPClient);
FreeAndNil(LJSONResponse);
FreeAndNil(LStrStream);
// Call the API
HTTPClient := TNetHTTPClient.Create(nil);
LJSONReq := TJSONObject.Create;
LJSONReq.AddPair('encodingType', 'UTF8');
LObj := TJSONObject.Create;
LObj.AddPair('type', 'PLAIN_TEXT');
LJSONReq.AddPair('document', LObj);
LFeat := TJSONObject.Create;
LFeat.AddPair('extractSyntax', TJSONBool.Create(true));
LFeat.AddPair('extractDocumentSentiment', TJSONBool.Create(true));
LJSONReq.AddPair('features', LFeat);
SetLength(LHeaders, 1);
LHeaders[0].Value := 'Bearer ' + LAccessToken;
LHeaders[0].Name := 'Authorization';
LOutputStream := TStringStream.Create;
LObj.AddPair('content', 'Hello, my name is Elise!');
HTTPClient.Post('https://language.googleapis.com/v1/documents:annotateText',
TStringStream.Create(LJSONReq.ToString), LOutputStream, LHeaders);
LStreamReader := TStreamReader.Create(LOutputStream);
Writeln(LStreamReader.ReadToEnd);
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.