|
1 | | -# `@microsoft/msgraph-sdk` |
| 1 | +# Microsoft Graph SDK for Typescript |
2 | 2 |
|
3 | | -> TODO: description |
| 3 | +Get started with the Microsoft Graph SDK for Typescript by integrating the [Microsoft Graph API](https://docs.microsoft.com/graph/overview) into your Typescript application! |
4 | 4 |
|
5 | | -## Usage |
| 5 | +> **Note:** this SDK allows you to build applications using the [v1.0](https://docs.microsoft.com/graph/use-the-api#version) of Microsoft Graph. If you want to try the latest Microsoft Graph APIs, use our [beta SDK](https://github.com/microsoftgraph/msgraph-beta-sdk-typescript) instead. |
6 | 6 |
|
| 7 | +> |
| 8 | +> **Note:** the Microsoft Graph Typescript SDK is currently in Pre-Release. |
| 9 | +
|
| 10 | +## 1. Installation |
| 11 | + |
| 12 | +```shell |
| 13 | +# this will install the main package |
| 14 | +npm install @microsoft/msgraph-sdk |
| 15 | +# this will install the authentication provider for Azure Identity / Microsoft Entra |
| 16 | +npm install @microsoft/kiota-authentication-azure @azure/identity |
| 17 | +# this will install the fluent API package for the users API paths (Replace with required package) |
| 18 | +npm install @microsoft/msgraph-sdk-users |
| 19 | +``` |
| 20 | + |
| 21 | +## 2. Getting started |
| 22 | + |
| 23 | +> Note: we are working to add the getting started information for Typescript to our public documentation, in the meantime the following sample should help you getting started. |
| 24 | +
|
| 25 | +### 2.1 Register your application |
| 26 | + |
| 27 | +Register your application by following the steps at [Register your app with the Microsoft Identity Platform](https://docs.microsoft.com/graph/auth-register-app-v2). |
| 28 | + |
| 29 | +### 2.2 Create an AuthenticationProvider object |
| 30 | + |
| 31 | +An instance of the **GraphServiceClient** class handles building client. To create a new instance of this class, you need to provide an instance of **AuthenticationProvider**, which can authenticate requests to Microsoft Graph. |
| 32 | + |
| 33 | +<!-- TODO restore that and remove the snippets below once the SDK hits GA and the public documentation has been updated --> |
| 34 | +<!-- For an example of how to get an authentication provider, see [choose a Microsoft Graph authentication provider](https://docs.microsoft.com/graph/sdks/choose-authentication-providers?tabs=typescript). --> |
| 35 | + |
| 36 | +#### 2.2.1 Authorization Code Provider |
| 37 | + |
| 38 | +```TypeScript |
| 39 | +// @azure/identity |
| 40 | +const credential = new AuthorizationCodeCredential( |
| 41 | + 'YOUR_TENANT_ID', |
| 42 | + 'YOUR_CLIENT_ID', |
| 43 | + 'YOUR_CLIENT_SECRET', |
| 44 | + 'AUTHORIZATION_CODE', |
| 45 | + 'REDIRECT_URL', |
| 46 | +); |
| 47 | + |
| 48 | +// @microsoft/kiota-authentication-azure |
| 49 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["User.Read"]); |
| 50 | +``` |
| 51 | + |
| 52 | +#### 2.2.2 Client Credentials Provider |
| 53 | + |
| 54 | +##### With a certificate |
| 55 | + |
| 56 | +```TypeScript |
| 57 | +// @azure/identity |
| 58 | +const credential = new ClientCertificateCredential( |
| 59 | + 'YOUR_TENANT_ID', |
| 60 | + 'YOUR_CLIENT_ID', |
| 61 | + 'YOUR_CERTIFICATE_PATH', |
| 62 | +); |
| 63 | + |
| 64 | +// @microsoft/kiota-authentication-azure |
| 65 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["https://graph.microsoft.com/.default"]); |
7 | 66 | ``` |
8 | | -const msgraphSdkJavascript = require('@microsoft/msgraph-sdk'); |
9 | 67 |
|
10 | | -// TODO: DEMONSTRATE API |
| 68 | +##### With a secret |
| 69 | + |
| 70 | +```TypeScript |
| 71 | +// @azure/identity |
| 72 | +const credential = new ClientSecretCredential( |
| 73 | + 'YOUR_TENANT_ID', |
| 74 | + 'YOUR_CLIENT_ID', |
| 75 | + 'YOUR_CLIENT_SECRET', |
| 76 | +); |
| 77 | + |
| 78 | +// @microsoft/kiota-authentication-azure |
| 79 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["https://graph.microsoft.com/.default"]); |
11 | 80 | ``` |
| 81 | + |
| 82 | +#### 2.2.3 On-behalf-of provider |
| 83 | + |
| 84 | +```TypeScript |
| 85 | +// @azure/identity |
| 86 | +const credential = new OnBehalfOfCredential({ |
| 87 | + tenantId: 'YOUR_TENANT_ID', |
| 88 | + clientId: 'YOUR_CLIENT_ID', |
| 89 | + clientSecret: 'YOUR_CLIENT_SECRET', |
| 90 | + userAssertionToken: 'JWT_TOKEN_TO_EXCHANGE', |
| 91 | +}); |
| 92 | + |
| 93 | +// @microsoft/kiota-authentication-azure |
| 94 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["https://graph.microsoft.com/.default"]); |
| 95 | +``` |
| 96 | + |
| 97 | +#### 2.2.4 Device code provider |
| 98 | + |
| 99 | +```TypeScript |
| 100 | +// @azure/identity |
| 101 | +const credential = new DeviceCodeCredential({ |
| 102 | + tenantId: 'YOUR_TENANT_ID', |
| 103 | + clientId: 'YOUR_CLIENT_ID', |
| 104 | + userPromptCallback: (info) => { |
| 105 | + console.log(info.message); |
| 106 | + }, |
| 107 | +}); |
| 108 | + |
| 109 | +// @microsoft/kiota-authentication-azure |
| 110 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["User.Read"]); |
| 111 | +``` |
| 112 | + |
| 113 | +#### 2.2.5 Interactive provider |
| 114 | + |
| 115 | +```TypeScript |
| 116 | +// @azure/identity |
| 117 | +const credential = new InteractiveBrowserCredential({ |
| 118 | + tenantId: 'YOUR_TENANT_ID', |
| 119 | + clientId: 'YOUR_CLIENT_ID', |
| 120 | + redirectUri: 'http://localhost', |
| 121 | +}); |
| 122 | + |
| 123 | +// @microsoft/kiota-authentication-azure |
| 124 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["User.Read"]); |
| 125 | +``` |
| 126 | + |
| 127 | +#### 2.2.6 Username/password provider |
| 128 | + |
| 129 | +```TypeScript |
| 130 | +// @azure/identity |
| 131 | +const credential = new UsernamePasswordCredential( |
| 132 | + 'YOUR_TENANT_ID', |
| 133 | + 'YOUR_CLIENT_ID', |
| 134 | + 'YOUR_USER_NAME', |
| 135 | + 'YOUR_PASSWORD', |
| 136 | +); |
| 137 | + |
| 138 | +// @microsoft/kiota-authentication-azure |
| 139 | +const authProvider = new AzureIdentityAuthenticationProvider(credential, ["User.Read"]); |
| 140 | +``` |
| 141 | + |
| 142 | +### 2.3 Get a Graph Service Client Adapter object |
| 143 | + |
| 144 | +You must get a **GraphServiceClient** object to make requests against the service. |
| 145 | + |
| 146 | +```typescript |
| 147 | +const requestAdapter = new GraphRequestAdapter(authProvider); |
| 148 | +const graphServiceClient = createGraphServiceClient(requestAdapter); |
| 149 | +``` |
| 150 | + |
| 151 | +## 3. Make requests against the service |
| 152 | + |
| 153 | +After you have a **GraphServiceClient** that is authenticated, you can begin making calls against the service. The requests against the service look like our [REST API](https://docs.microsoft.com/graph/api/overview?view=graph-rest-1.0). |
| 154 | + |
| 155 | +### 3.1 Get user's detailed information |
| 156 | + |
| 157 | +To retrieve the user's detailed information: |
| 158 | + |
| 159 | +```typescript |
| 160 | +import { createGraphServiceClient, GraphRequestAdapter } from "@microsoft/msgraph-sdk"; |
| 161 | +import "@microsoft/msgraph-sdk-users"; |
| 162 | + |
| 163 | +const requestAdapter = new GraphRequestAdapter(authProvider); |
| 164 | +const graphServiceClient = createGraphServiceClient(requestAdapter); |
| 165 | + |
| 166 | +const jane = await graphServiceClient.users.byUserId("jane@contoso.com").get(); |
| 167 | +``` |
| 168 | + |
| 169 | +## 4. Documentation |
| 170 | + |
| 171 | +For more detailed documentation, see: |
| 172 | + |
| 173 | +* [Overview](https://docs.microsoft.com/graph/overview) |
| 174 | +* [Collections](https://docs.microsoft.com/graph/sdks/paging) |
| 175 | +* [Making requests](https://docs.microsoft.com/graph/sdks/create-requests) |
| 176 | +* [Known issues](https://github.com/MicrosoftGraph/msgraph-sdk-typescript/issues) |
| 177 | +* [Contributions](https://github.com/microsoftgraph/msgraph-sdk-typescript/blob/main/CONTRIBUTING.md) |
| 178 | + |
| 179 | +## 5. Issues |
| 180 | + |
| 181 | +For known issues, see [issues](https://github.com/MicrosoftGraph/msgraph-sdk-typescript/issues). |
| 182 | + |
| 183 | +## 6. Contributions |
| 184 | + |
| 185 | +The Microsoft Graph SDK is open for contribution. To contribute to this project, see [Contributing](https://github.com/microsoftgraph/msgraph-sdk-typescript/blob/main/CONTRIBUTING.md). |
| 186 | + |
| 187 | +## 7. License |
| 188 | + |
| 189 | +Licensed under the [MIT license](https://github.com/microsoftgraph/msgraph-sdk-typescript/blob/main/LICENSE). |
| 190 | + |
| 191 | +## 8. Third-party notices |
| 192 | + |
| 193 | +[Third-party notices](https://github.com/microsoftgraph/msgraph-sdk-typescript/blob/main/LICENSE) |
0 commit comments