pnp / graph-connectors-samples

Microsoft Graph Connector samples
https://aka.ms/gc/samples
MIT License
41 stars 32 forks source link

Connect to REST API using OAuth2 #45

Closed daniel-alsen closed 3 months ago

daniel-alsen commented 6 months ago

Thank you for creating this useful sample library!

I used the REST Countries sample with Powershell as basis for a Graph Connector used for Copilot. Would it be possible to complement that sample with a few lines of code describing how to connect via PowerShell if the REST API uses OAuth2 with a token?

Best regards, Daniel

waldekmastykarz commented 6 months ago

Thank you for the kind words, @daniel-alsen!

Since the sample you mentioned refers to an anonymous API, I suggest that we keep it as-is and instead work on creating a new sample. Is this something that you'd like to work on or are you rather looking for an example of how to solve it?

daniel-alsen commented 6 months ago

I'm new with PowerShell and GraphConnectors, so I'm looking for a sample in this case. But happy to contribute with other samples in the future when I get my arms around this :)

waldekmastykarz commented 6 months ago

@nanddeepn, @smitanachan, see the question from @daniel-alsen above. Have you by any chance worked on creating a Graph connector in PowerShell, that's connected to an API behind OAuth? If so, would you be willing to share it? If not, is this something that perhaps you'd like to work together on to share with others? 😊

daniel-alsen commented 6 months ago

I haven't written anything in PowerShell, but I wrote the following code with help from ChatGPT, and it works. Feel free to use it in the commentary of the sample if you want.

$authURL = "https://xxx/oauth/token";

$creds = @{ client_id = "XXX" client_secret = "XXX" };

$authHeader = @{"content-type" = "application/x-www-form-urlencoded"}

$response = Invoke-RestMethod "$authURL" -Method Post -Body $creds -Headers $authHeader

$token = $response.access_token; $authHeader = @{"Accept" = "application/json" ; "Authorization" = "Bearer $token"}

$apiUrl = "https://xxx";

$json = Invoke-RestMethod -Uri "$apiUrl" -Method Get -ContentType "application/json" -Header $authHeader

nanddeepn commented 6 months ago

Hi @daniel-alsen If you are looking for a sample to use App registration with App ID and Secret, you may refer this sample: https://pnp.github.io/script-samples/teams-list-installed-apps/README.html?tabs=ps

daniel-alsen commented 6 months ago

Thank you Nanddeep!

waldekmastykarz commented 3 months ago

Considering completed due to lack of further comments