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 101 102 103 104 105 106 107 108 109 110 111 112 113
|
procedure TForm1.Button1Click(Sender: TObject);
begin
RESTDemandeCode;
end;
function TForm1.GetSimpleValue(aUrl:string;aVar:string):string;
begin
Result :='';
if ContainsText(aUrl, aVar + '=') then begin
Result := Copy(aUrl, Pos(aVar + '=', aUrl) + Length(aVar) + 1, Length(aUrl));
if (Pos('&', Result) > 0) then
Result := Copy(Result, 1, Pos('&', Result) - 1);
end;
end;
procedure TForm1.RESTDemandeCode;
var
vUrl : string;
vTenantID : string;
vClientID : string;
vRedirection : string;
vScope : string;
vAuthorizationEndpoint : string;
begin
vTenantID := 'MonTenant';
vClientID := 'MonClientID';
vRedirection := System.NetEncoding.TURLEncoding.URL.Encode('https://localhost');
vScope := 'https://graph.microsoft.com/.default';
vAuthorizationEndpoint := 'https://login.microsoftonline.com/' + vTenantID + '/oauth2/v2.0/authorize';
vUrl := vAuthorizationEndpoint;
vUrl := vUrl + '?client_id=' + vClientID;
vUrl := vUrl + '&response_type=code';
vUrl := vUrl + '&redirect_uri=' + vRedirection;
vUrl := vUrl + '&scope=' + vScope;
WebBrowser1.URL := vUrl;
end;
procedure TForm1.WebBrowser1DidFinishLoad(ASender: TObject);
var
vCode : String;
begin
Memo1.Lines.Add(WebBrowser1.URL);
vCode := GetSimpleValue(WebBrowser1.Url,'code');
if vCode <>'' then begin
Code := vCode;
Code_lbl.Text := vCode;
end;
end;
procedure TForm1.ConfigureProxy(aClient: TCustomRESTClient);
begin
aClient.ProxyServer := '';
aClient.ProxyPort := 0 ;
aClient.ProxyUsername := '';
aClient.ProxyPassword := '';
end;
procedure TForm1.RESTDemandeToken;
var
vRESTClient : TRESTClient;
vRESTRequest : TRESTRequest;
vTenantID : string;
vClientID, vClientSecret : string;
vRedirection : string;
vAccessTokenEndpoint : string;
vReponse : String;
begin
vTenantID := 'MonTenant';
vClientID := 'MonClientID';
vClientSecret := 'MonClientSecret';
vRedirection := 'https://localhost';
vAccessTokenEndpoint := 'https://login.microsoftonline.com/' + vTenantID + '/oauth2/v2.0/token';
vRESTClient := TRESTClient.Create(self);
ConfigureProxy(vRESTClient);
vRESTRequest := TRESTRequest.Create(self);
vRESTRequest.Client := vRESTClient;
vRESTRequest.Method := TRESTRequestMethod.rmPost;
vRESTClient.BaseURL := vAccessTokenEndpoint;
vRESTRequest.AddParameter('code', code);
vRESTRequest.AddParameter('client_id', vClientID);
vRESTRequest.AddParameter('client_secret', vClientSecret);
vRESTRequest.AddParameter('grant_type', 'authorization_code');
vRESTRequest.AddParameter('redirect_uri', vRedirection);
Try
vRESTRequest.Execute;
if (vRESTRequest.Response.StatusCode = 200) then begin
vRESTRequest.Response.GetSimpleValue('access_token',vReponse);
Token_lbl.Text := vReponse;
end;
Finally
FreeAndNIL(vRESTRequest);
FreeAndNIL(vRESTClient);
End;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
RESTDemandeToken;
end; |
Partager