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

CORS issue #721

Open
TomTomDam opened this issue Jan 19, 2025 · 2 comments
Open

CORS issue #721

TomTomDam opened this issue Jan 19, 2025 · 2 comments

Comments

@TomTomDam
Copy link

TomTomDam commented Jan 19, 2025

Hi, I am trying to get a working example of this package as outlined in the README and using a fresh React app, but I am getting a CORS issue. Am I missing a step?

Image

My code:

import reactLogo from "./assets/react.svg";
import viteLogo from "/vite.svg";
import "./App.css";
import { FetchRequestAdapter } from "@microsoft/kiota-http-fetchlibrary";
import { createGraphServiceClient } from "@microsoft/msgraph-sdk";
import "@microsoft/msgraph-sdk-users";
import { ClientSecretCredential } from "@azure/identity";
import { AzureIdentityAuthenticationProvider } from "@microsoft/kiota-authentication-azure";

function App() {
  const [count, setCount] = useState(0);

  const getUsers = async () => {
    // @azure/identity
    const credential = new ClientSecretCredential(
      "TENANT ID",
      "CLIENT ID",
      "CLIENT SECRET"
    );

    // @microsoft/kiota-authentication-azure
    const authProvider = new AzureIdentityAuthenticationProvider(credential, [
      "https://graph.microsoft.com/.default",
    ]);

    const requestAdapter = new FetchRequestAdapter(authProvider);
    const graphServiceClient = createGraphServiceClient(requestAdapter);

    try {
      const users = await graphServiceClient.users.get();
      console.log(JSON.stringify(users));
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <>
      <div>
        <a href="https://vite.dev" target="_blank">
          <img src={viteLogo} className="logo" alt="Vite logo" />
        </a>
        <a href="https://react.dev" target="_blank">
          <img src={reactLogo} className="logo react" alt="React logo" />
        </a>
      </div>
      <h1>Vite + React</h1>
      <div>
        <button onClick={getUsers}>Get users</button>
      </div>
      <div className="card">
        <button onClick={() => setCount((count) => count + 1)}>
          count is {count}
        </button>
        <p>
          Edit <code>src/App.tsx</code> and save to test HMR
        </p>
      </div>
      <p className="read-the-docs">
        Click on the Vite and React logos to learn more
      </p>
    </>
  );
}

export default App;

I have also made sure to set up my Azure app registration and I have added a SPA redirect URL, the URL being http://localhost:5173/ which is my app's URL.

@baywet
Copy link
Member

baywet commented Jan 20, 2025

Hi @TomTomDam
Thank you for using the SDK and for reaching out.

My guess is that your application registration is not enabled for SPA OR you have not registered your redirect uris.

more information

Let us know if you have any additional comments or questions.

@baywet baywet added question Further information is requested Needs: Author Feedback status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close type:question An issue that's a question and removed ToTriage labels Jan 20, 2025
@TomTomDam
Copy link
Author

TomTomDam commented Jan 25, 2025

@baywet I already created the app registration and registered the redirect URL using this step: Redirect URI: MSAL.js 2.0 with auth code flow. I ticked "Access tokens" for this.

I also have my Supported account types set to: Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant)

But I am still getting the same issue. Would the options I mentioned affect anything?

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs: Attention 👋 and removed status:waiting-for-author-feedback Issue that we've responded but needs author feedback to close labels Jan 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants