Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect to REST API using OAuth2 #45

Closed
daniel-alsen opened this issue Feb 27, 2024 · 7 comments
Closed

Connect to REST API using OAuth2 #45

daniel-alsen opened this issue Feb 27, 2024 · 7 comments
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@daniel-alsen
Copy link

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
Copy link
Member

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
Copy link
Author

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 waldekmastykarz added good first issue Good for newcomers help wanted Extra attention is needed labels Feb 28, 2024
@waldekmastykarz
Copy link
Member

@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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

Thank you Nanddeep!

@waldekmastykarz
Copy link
Member

Considering completed due to lack of further comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants