> ## Documentation Index
> Fetch the complete documentation index at: https://api-docs.codecombat.com/llms.txt
> Use this file to discover all available pages before exploring further.

> Adds an OAuth2 identity to the user, so that they can be logged in with that identity. You need to send the OAuth code or the access token to this endpoint.

# Add Oauth2 Identity

1. If no access token is provided, it will use your OAuth2 token URL to exchange
   the given code for an access token.
2. Then it will use the access token (given by you, or received from step 1) to
   look up the user on your service using the lookup URL, and expects a JSON
   object in response with an id property.
3. It will then save that user id to the user in our db as a new OAuthIdentity.

```python theme={null}
url = `https://codecombat.com/api/users/${userID}/o-auth-identities`;
OAUTH_PROVIDER_ID = "xyz";
json = { provider: OAUTH_PROVIDER_ID, accessToken: "1234" };
request.post({ url, json, auth }, (err, res) => {
  console.log(res.body.oAuthIdentities); // [ { provider: 'xyx', id: 'abcd' } ]
});
```

In this example, we call your lookup URL (let’s say,
`https://oauth.provider/user?t=<%= accessToken %>`) with the access token
(1234). The lookup URL returns `{ id: 'abcd' }` in this case, which we save to
the user in our db.


## OpenAPI

````yaml POST /users/{handle}/o-auth-identities
openapi: 3.0.1
info:
  title: CodeCombat API
  version: 0.0.1
servers:
  - url: https://codecombat.com/api
    description: default
security: []
paths:
  /users/{handle}/o-auth-identities:
    post:
      tags:
        - ''
      summary: Add Oauth2 Identity
      description: >
        Adds an OAuth2 identity to the user, so that they can be logged in with
        that identity. You need to send the OAuth code or the access token to
        this endpoint. 1. If no access token is provided, it will use your
        OAuth2 token URL to exchange the given code for an access token. 2. Then
        it will use the access token (given by you, or received from step 1) to
        look up the user on your service using the lookup URL, and expects a
        JSON object in response with an `id` property. 3. It will then save that
        user `id` to the user in our db as a new OAuthIdentity. In this example,
        we call your lookup URL (let's say, `https://oauth.provider/user?t=<%=
        accessToken %>`) with the access token (`1234`). The lookup URL returns
        `{ id: 'abcd' }` in this case, which we save to the user in our db.
      operationId: postUsersHandleOAuthIdentities
      parameters:
        - name: handle
          in: path
          description: The document's `_id` or `slug`.
          required: true
          schema:
            type: string
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                provider:
                  type: string
                  description: Your OAuth Provider ID.
                accessToken:
                  type: string
                  nullable: true
                  description: >-
                    Will be passed through your lookup URL to get the user ID.
                    Required if no `code`.
                code:
                  type: string
                  nullable: true
                  description: >-
                    Will be passed to the OAuth token endpoint to get a token.
                    Required if no `accessToken`.
              required:
                - provider
      responses:
        '200':
          description: The affected user
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/UserResponse'
      security:
        - BasicAuth: []
components:
  schemas:
    UserResponse:
      title: UserResponse
      type: object
      description: Subset of properties listed here
      properties:
        _id:
          $ref: '#/components/schemas/objectIdString'
          nullable: true
        email:
          type: string
          nullable: true
        name:
          type: string
          nullable: true
        slug:
          type: string
          nullable: true
        role:
          $ref: '#/components/schemas/roleString'
          nullable: true
        stats:
          $ref: '#/components/schemas/UserResponseStats'
          nullable: true
        oAuthIdentities:
          type: array
          items:
            $ref: '#/components/schemas/UserResponseOAuthIdentitiesItem'
          nullable: true
        subscription:
          $ref: '#/components/schemas/UserResponseSubscription'
          nullable: true
        license:
          $ref: '#/components/schemas/UserResponseLicense'
          nullable: true
    objectIdString:
      title: objectIdString
      type: string
    roleString:
      title: roleString
      type: string
      description: Usually either 'teacher' or 'student'
    UserResponseStats:
      title: UserResponseStats
      type: object
      properties:
        gamesCompleted:
          type: number
          format: double
          nullable: true
        concepts:
          type: object
          additionalProperties:
            type: number
            format: double
          nullable: true
        playTime:
          type: number
          format: double
          nullable: true
          description: Included only when specifically requested on the endpoint
    UserResponseOAuthIdentitiesItem:
      title: UserResponseOAuthIdentitiesItem
      type: object
      properties:
        provider:
          type: string
          nullable: true
        id:
          type: string
          nullable: true
    UserResponseSubscription:
      title: UserResponseSubscription
      type: object
      properties:
        ends:
          $ref: '#/components/schemas/datetimeString'
          nullable: true
        active:
          type: boolean
          nullable: true
    UserResponseLicense:
      title: UserResponseLicense
      type: object
      properties:
        ends:
          $ref: '#/components/schemas/datetimeString'
          nullable: true
        active:
          type: boolean
          nullable: true
    datetimeString:
      title: datetimeString
      type: string
  securitySchemes:
    BasicAuth:
      type: http
      scheme: basic

````