- Node.js installed
- npm or yarn installed
-
Clone the repository:
git clone https://github.com/rkodev/msgraph-ts-test.git cd msgraph-ts-test
-
Install dependencies:
npm install
or
yarn install
-
Compile TypeScript:
npm run build
or
yarn build
-
Configure Microsoft Graph Client: Modify the file
auth.ts
and replace the auth provider with the appropriate details:// @azure/identity const credential = new UsernamePasswordCredential( 'YOUR_TENANT_ID', 'YOUR_CLIENT_ID', 'YOUR_USER_NAME', 'YOUR_PASSWORD', ); // @microsoft/kiota-authentication-azure const authProvider = new AzureIdentityAuthenticationProvider(credential, ["User.Read"]); export const requestAdapter = new FetchRequestAdapter(authProvider); export const graphServiceClient = createGraphServiceClient(requestAdapter);
Hint If you do not have a tenant set up, you can use an access token with graph explorer and use
simple_credentials.ts
to provide a authentication.i. Go to GE and obtain a token ii. Add it to the value `const tokenValue = "";` iii. In index.ts, replace "import { graphServiceClient } from "./auth";" with "import { graphServiceClient } from "./simple_credentials";"
-
Install Microsoft Graph SDK package
npm install @microsoft/msgraph-sdk-users
or
yarn add @microsoft/msgraph-sdk-users
Hint The required package for the section of graph you want to test should always be installed i.e
Section Package Install Command Import Statement Users @microsoft/msgraph-sdk-users npm install @microsoft/msgraph-sdk-users import "@microsoft/msgraph-sdk-users"; Groups @microsoft/msgraph-sdk-groups npm install @microsoft/msgraph-sdk-groups import "@microsoft/msgraph-sdk-groups"; Mail @microsoft/msgraph-sdk-mail npm install @microsoft/msgraph-sdk-mail import "@microsoft/msgraph-sdk-mail"; Files @microsoft/msgraph-sdk-files npm install @microsoft/msgraph-sdk-files import "@microsoft/msgraph-sdk-files"; -
Make a call to Microsoft Graph: In your TypeScript file, you can now use the configured client to make calls, ensure that the installed package for the graph subsection is always impoted.
import "@microsoft/msgraph-sdk-users"; // this is necessary to make calls for this section import { graphServiceClient } from "./auth"; async function main() { console.log("Getting me"); const result = await graphServiceClient.me.get(); console.log(result?.displayName); } main().catch(console.error);
-
Execute the project: To execute the project you first need to build it using
npm run build
Then run the index file using
node dist/index.js
- Ensure you have the correct permissions to clone the repository.
- Adjust the commands as per your package manager (npm or yarn).
- Replace
YOUR_CLIENT_ID
andYOUR_TENANT_ID
with your actual Azure AD application credentials. - Ensure you have the necessary permissions and scopes configured in your Azure AD application.
- Follow the Microsoft Graph SDK documentation for more details.