# wordpress-rest-api-oauth-1

> WordPrest REST API OAuth 1 Client

Latest version **1.1.7** (published 2017-05-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install wordpress-rest-api-oauth-1
pnpm add wordpress-rest-api-oauth-1
yarn add wordpress-rest-api-oauth-1
bun add wordpress-rest-api-oauth-1
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2017-05-15 |
| First published | 2016-07-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 35 |
| Author | joehoyle |
| Maintainers | joehoyle, rmccue |

## Links

- npm: https://www.npmjs.com/package/wordpress-rest-api-oauth-1
- Repository: https://github.com/WP-API/wordpress-rest-api-oauth-1
- Homepage: https://github.com/WP-API/wordpress-rest-api-oauth-1#readme
- Issues: https://github.com/WP-API/wordpress-rest-api-oauth-1/issues
- npm.io page: https://npm.io/package/wordpress-rest-api-oauth-1

## Dependencies (2)

- [qs](https://npm.io/package/qs.md) ^6.2.0
- [oauth-1.0a](https://npm.io/package/oauth-1.0a.md) ^1.0.1

## Recent versions

- 1.1.7 (latest) — 2017-05-15
- 1.1.6 — 2017-04-24
- 1.1.5 — 2017-03-26
- 1.1.4 — 2017-03-26
- 1.1.3 — 2017-03-10
- 1.1.2 — 2017-03-09
- 1.1.1 — 2017-03-07
- 1.1.0 — 2017-03-01
- 1.0.8 — 2016-07-31
- 1.0.7 — 2016-07-31
- 1.0.6 — 2016-07-28
- 1.0.5 — 2016-07-17
- 1.0.4 — 2016-07-15
- 1.0.3 — 2016-07-15
- 1.0.2 — 2016-07-15
- … 2 more at https://npm.io/package/wordpress-rest-api-oauth-1/versions

## README

# WordPress REST API OAuth 1 Client

JavaScript OAuth 1 Client for the WordPress REST API v2.

## Install

```
npm install --save wordpress-rest-api-oauth-1
```

## Configuration

### Without Authentication

```JS
import api from 'wordpress-rest-api-oauth-1'

const demoApi = new api({
	url: 'https://demo.wp-api.org/'
})
```
### Using OAuth 1 Directly

To communication and authenticate using OAuth 1 with your WordPress site directly:

```JS
import api from 'wordpress-rest-api-oauth-1'

const demoApi = new api({
	url: 'https://demo.wp-api.org/',
	credentials: {
		client: {
			public: 'xxxxxx',
			secret: 'xxxxxxxxxxxxxxxxxxx'
		}
	}
})
```

### Using the WordPress Authentication Broker

To establish a connection to a WordPress site that accepts the [WordPress REST API Broker](https://apps.wp-api.org/):

```JS
import api from 'wordpress-rest-api-oauth-1'

const demoApi = new api({
	url: 'https://demo.wp-api.org/',
	brokerCredentials: {
		client: {
			public: 'xxxxxx',
			secret: 'xxxxxxxxxxxxxxxxxxx'
		}
	}
})

// Get OAuth client tokens for the specified site. This is not needed if using `authorize()`.
demoApi.getConsumerToken().then( token => {
	console.log( token )
})
```

### Usage

#### Authorize / OAuth Flow

There is two ways to get authentication tokens, one "high level" function, or you can implement your own flow using the underlaying function.

##### The Quick Way

```JS
demoApi.authorize().then( function() {
	console.log( 'All API requests are now authenticated.' )
})

// Note: the above will cause a redirect / resume of the app in the event that the user needs to authorize.
```

##### Control your own flow

```JS
// Get client tokens from the broker (optional)
demoApi.getConsumerToken().then( ... )

// Get a request token
demo.getRequestToken() )
	.then( token => {
		// handle the user authorize redirect with token.redirectURL
	})

// Exchange for an access token
demo.getAccessToken( oAuthVerifier )
	.then( token => {
		// save the token to localStorage etc.
	})
```

#### Make API Requests

You can make API requests directly with this library for both authenticated requests and anonymous.


```JS
demoApi.get( '/wp/v2/posts', { per_page: 5 } ).then( posts => {
	console.log( posts )
})

demoApi.post( '/wp/v2/posts', { title: 'Test new post' } } ).then( post => {
	console.log( post )
})

demoApi.del( '/wp/v2/posts/1' ).then( post => {
	console.log( 'Deleted post.' )
})
```

### Loading and Saving Credentials

With OAuth in the browser, you don't typically want to run through the authorization flow on every page load, so you can export and import the credentials if you wish:

```JS
// init API with credentials:
new api({
	url: siteURL,
	credentials: JSON.parse( localStorage.getItem( 'authCredentials' ) )
})

// save the credentials
localStorage.setItem( 'authCredentials', JSON.stringify( demoApi.config.credentials ) )
```

You can also have the library store and retrieve the credentials:

```JS
demoApi.restoreCredentials().get( '/wp/v2/users/me' )

demoApi.saveCredentials() // Save the credentials to localStorage
```

To implement restoring of credentials and auth in one go:

```JS
demoApi.restoreCredentials().authorize().then( function() {
	demoApi.saveCredentials()
})
```

---
_Source: https://npm.io/package/wordpress-rest-api-oauth-1 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
