Giter VIP home page Giter VIP logo

Comments (5)

FredTheDane avatar FredTheDane commented on July 17, 2024 3

As suggested, adding in the getSsoToken function made all the difference and now I can query my API with no issues! :)

@JerryYangKai and @blackchoey thank you both for the great help! πŸš€ πŸš€

from teams-toolkit.

blackchoey avatar blackchoey commented on July 17, 2024

Hi @FredTheDane , can you share how do you get the authentication token retrieved from Teams inside the tab you mentioned? By the way, does the internal Web API mean an API implemented in same project as your tab app? Or it means another web API project?
By default, the TeamsFx extension configured the web api to accept token got from Teams Tab single sign-on feature (you can use TeamsUserCredential to get such a token). You can find related configuration code at https://github.com/OfficeDev/TeamsFx/blob/dev/packages/simpleauth/src/TeamsFxSimpleAuth/SimpleAuthWebApiExtension.cs#L39-L55

from teams-toolkit.

FredTheDane avatar FredTheDane commented on July 17, 2024

Hi @blackchoey :)

The way that I try and use the Teams Authentication token is something along the lines of this inside the OnAfterRenderAsync method of the tab.razor file (The application is pretty much as is with what you get using the Microsoft Teams App project template:

            var token = await teamsfx.GetAuthenticationToken();
            WebClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.Token);
            var value = await WebClient.GetAsync("https://localhost:44357/api/Diagram");

The Diagram api is just a quick and dirty API controller that spits out an array within the same project as the tab app. However, when checking the value I get this Unauthorized error back: {Bearer error="invalid_token", error_description="The signature is invalid"}

When you say that I can use TeamUserCredential to get a token, where would I need to implement this in the JwtBearerOptions and how would I get the token?

from teams-toolkit.

blackchoey avatar blackchoey commented on July 17, 2024

Thank you @FredTheDane for the details. The project created by Teams Toolkit extension wraps the TeamsUserCredential to acquire token. @JerryYangKai will provide more instructions on how to update the code to call the internal web APIs.

from teams-toolkit.

JerryYangKai avatar JerryYangKai commented on July 17, 2024

Hi @FredTheDane, since we only got JavaScript version of microsoft/teamsfx sdk, so we including webpack to wrapper it and using JSRuntime to trigger it in Blazor.
The webpack source code is in JS/src/index.js, you could add these code to get SSO token

export async function getSsoToken() {
var credential = new TeamsUserCredential();
var token = await credential.getToken("");
return token;
}

Then under JS folder , run 'npm install' and 'npm run build' to webpack the code and refresh the webpack file (wwwroot/teamsfx.js).
Finally you could using JSRuntime trigger the new func you just added in TeamsFx.cs.
await jsRuntime.InvokeAsync<AccessToken>("TeamsFx.getSsoToken");
We've tried to remove webpack and provide a more convenient way to using sdk in the future.

from teams-toolkit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.