# oauth-methods.js > Set of stateless request methods to create, check, reset, refresh, and delete user access tokens for OAuth and GitHub Apps [](https://www.npmjs.com/package/@octokit/oauth-methods) [](https://github.com/octokit/oauth-methods.js/actions?query=workflow%3ATest+branch%3Amain) - [Usage](#usage) - [OAuth Web Flow](#oauth-web-flow) - [OAuth Device Flow](#oauth-device-flow) - [Methods](#methods) - [`getWebFlowAuthorizationUrl()`](#getwebflowauthorizationurl) - [`exchangeWebFlowCode()`](#exchangewebflowcode) - [`createDeviceCode()`](#createdevicecode) - [`exchangeDeviceCode()`](#exchangedevicecode) - [`checkToken()`](#checktoken) - [`refreshToken()`](#refreshtoken) - [`scopeToken()`](#scopetoken) - [`resetToken()`](#resettoken) - [`deleteToken()`](#deletetoken) - [`deleteAuthorization()`](#deleteauthorization) - [Authentication object](#authentication-object) - [OAuth APP authentication](#oauth-app-authentication) - [GitHub App with non-expiring user authentication](#github-app-with-non-expiring-user-authentication) - [GitHub App with expiring user authentication](#github-app-with-expiring-user-authentication) - [Types](#types) - [Contributing](#contributing) - [License](#license) The OAuth endpoints related to user access tokens are not all part of GitHub's REST API and they behave slightly different. The methods exported by `@octokit/normalize the differences so you don't have to. ## Usage
| Browsers | `@octokit/oauth-methods` is not meant for browser usage. Some of the methods will work, but others do not have CORS headers enabled and will fail (`exchangeWebFlowCode()`, `createDeviceCode()`, `exchangeDeviceCode()`, `refreshToken()`). Also the Client Secret should not be exposed to clients as it can be used for a [Person-in-the-middle attack](https://en.wikipedia.org/wiki/Person-in-the-middle_attack). |
|---|---|
| Node | Install with `npm install @octokit/core @octokit/oauth-methods` ```js const { exchangeWebFlowCode, createDeviceCode, exchangeDeviceCode, checkToken, refreshToken, scopeToken, resetToken, deleteToken, deleteAuthorization, } = require("@octokit/oauth-methods"); ``` |
| name | type | description |
|---|---|---|
clientId
|
string
|
Required. The client ID you received from GitHub when you registered. |
clientType
|
string
|
Required. Must be set to either "oauth-app" or "github-app".
|
redirectUrl
|
string
|
The URL in your application where users will be sent after authorization. See Redirect URLs in GitHub’s Developer Guide. |
login
|
string
|
Suggests a specific account to use for signing in and authorizing the app. |
scopes
|
array of strings
|
Only relevant if `clientType` is set to `"oauth-app"`. An array of scope names (or: space-delimited list of scopes). If not provided, scope defaults to an empty list for users that have not authorized any scopes for the application. For users who have authorized scopes for the application, the user won't be shown the OAuth authorization page with the list of scopes. Instead, this step of the flow will automatically complete with the set of scopes the user has authorized for the application. For example, if a user has already performed the web flow twice and has authorized one token with user scope and another token with repo scope, a third web flow that does not provide a scope will receive a token with user and repo scope. Defaults to `[]`. |
state
|
string
|
An unguessable random string. It is used to protect against cross-site request forgery attacks.
Defaults to Math.random().toString(36).substr(2).
|
allowSignup
|
boolean
|
Whether or not unauthenticated users will be offered an option to sign up for GitHub during the OAuth flow. Use false in the case that a policy prohibits signups. Defaults to true.
|
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { url } = getWebFlowAuthorizationUrl({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
scopes: ["repo"],
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
allowSignup
|
boolean
|
Returns options.allowSignup if it was set. Defaults to true.
|
clientType
|
string
|
Returns options.clientType
|
clientId
|
string
|
Returns options.clientId.
|
login
|
string
|
Returns options.login if it was set. Defaults to null.
|
redirectUrl
|
string
|
Returns options.redirectUrl if it was set. Defaults to null.
|
scopes
|
array of strings
|
Only set if `options.clientType` is set to `"oauth-app"`.
Returns an array of strings. Returns options.scopes if it was set and turns the string into an array if a string was passed, otherwise [].
|
state
|
string
|
Returns options.state if it was set. Defaults to Math.random().toString(36).substr(2).
|
url
|
string
|
The authorization URL |
| name | type | description |
|---|---|---|
clientType
|
string
|
Required. Must be set to either "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
code
|
string
|
Required. The code from GitHub's OAuth flow redirect's ?code=... query parameter
|
redirectUrl
|
string
|
The redirectUrl option you passed to getWebFlowAuthorizationUrl()
|
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await exchangeWebFlowCode({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef12347890abcdef12345678",
code: "code123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Required. Must be set to either "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
scopes
|
array of strings
|
Only permitted if `clientType` is set to `"oauth-app"`. GitHub Apps do not support scopes. Array of [scope names](https://docs.github.com/en/developers/apps/scopes-for-oauth-apps#available-scopes) you want to request for the user access token. |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data } = await createDeviceCode({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
scopes: ["repo"],
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Required. Must be set to either "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
code
|
string
|
Required. The decive_code from the createDeviceCode() response
|
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await exchangeDeviceCode({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
code: "code123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Required. Must be set to either "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
token
|
string
|
Required. The user access token to check |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await checkToken({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef12347890abcdef12345678",
token: "usertoken123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Must be set to "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
refreshToken
|
string
|
Required. The refresh token that was received alongside the user access token. |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await refreshToken({
clientType: "github-app",
clientId: "lv1.1234567890abcdef",
clientSecret: "1234567890abcdef12347890abcdef12345678",
refreshToken: "r1.refreshtoken123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Required. Must be set to "github-app".
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
target
|
string
|
Required unless targetId is set. The name of the user or organization to scope the user-to-server access token to.
|
targetId
|
integer
|
Required unless target is set. The ID of the user or organization to scope the user-to-server access token to.
|
repositories
|
array of strings
|
The list of repository names to scope the user-to-server access token to. repositories may not be specified if repository_ids is specified.
|
repository_ids
|
array of integers
|
The list of repository IDs to scope the user-to-server access token to. repositories may not be specified if repositories is specified.
|
permissions
|
object
|
The permissions granted to the user-to-server access token. See GitHub App Permissions. |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await scopeToken({
clientType: "github-app",
clientId: "lv1.1234567890abcdef",
token: "usertoken123",
target: "octokit",
repositories: ["oauth-methods.js"],
permissions: {
issues: "write",
},
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Must be set to "oauth-app" or "github-app".
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
token
|
string
|
Required. The user access token to reset |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await resetToken({
clientId: "1234567890abcdef1234",
clientSecret: "secret",
token: "usertoken123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Must be set to "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
token
|
string
|
Required. The user access token to delete |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await deleteToken({
clientId: "1234567890abcdef1234",
clientSecret: "secret",
token: "usertoken123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
Must be set to "oauth-app" or "github-app"
|
clientId
|
string
|
Required. Your app's client ID |
clientSecret
|
string
|
Required. One of your app's client secrets |
token
|
string
|
Required. A valid user access token for the authorization |
request
|
function
|
You can pass in your own @octokit/request instance. For usage with enterprise, set baseUrl to the REST API root endpoint. Example:
```js
const { request } = require("@octokit/request");
const { data, authentication } = await deleteAuthorization({
clientId: "1234567890abcdef1234",
clientSecret: "secret",
token: "usertoken123",
request: request.defaults({
baseUrl: "https://ghe.my-company.com/api/v3",
}),
});
```
|
| name | type | description |
|---|---|---|
clientType
|
string
|
"oauth-app"
|
clientId
|
string
|
The app's Client ID
|
token
|
string
|
The user access token |
scopes
|
array of strings
|
array of scope names enabled for the token |
| name | type | description |
|---|---|---|
clientType
|
string
|
"github-app"
|
clientId
|
string
|
The app's Client ID
|
token
|
string
|
The user access token |
| name | type | description |
|---|---|---|
clientType
|
string
|
"github-app"
|
clientId
|
string
|
The app's Client ID
|
token
|
string
|
The user access token |
refreshToken
|
string
|
The refresh token |
expiresAt
|
string
|
Date timestamp in ISO 8601 standard. Example: 2022-01-01T08:00:0.000Z
|
refreshTokenExpiresAt
|
string
|
Date timestamp in ISO 8601 standard. Example: 2021-07-01T00:00:0.000Z
|