Skip to content

Commit c5af135

Browse files
authored
Merge pull request #91 from Kit/get-access-token-by-api-secret-tenant-name-support
Add `tenant_name` support for `accounts/oauth_access_token`
2 parents ef5ffa9 + f19e919 commit c5af135

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

src/class-convertkit-api-v4.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -481,21 +481,25 @@ public function refresh_token() {
481481
*
482482
* @since 2.0.0
483483
*
484-
* @param string $api_key API Key.
485-
* @param string $api_secret API Secret.
484+
* @param string $api_key API Key.
485+
* @param string $api_secret API Secret.
486+
* @param bool|string $tenant_name Tenant Name (if specified, issues tokens specific to that name. Useful for using the same account on multiple sites).
486487
* @return WP_Error|array
487488
*/
488-
public function get_access_token_by_api_key_and_secret( $api_key, $api_secret ) {
489+
public function get_access_token_by_api_key_and_secret( $api_key, $api_secret, $tenant_name = '' ) {
489490

490-
return $this->post(
491-
'accounts/oauth_access_token',
492-
array(
493-
'api_key' => $api_key,
494-
'api_secret' => $api_secret,
495-
'client_id' => $this->client_id,
496-
)
491+
$args = array(
492+
'api_key' => $api_key,
493+
'api_secret' => $api_secret,
494+
'client_id' => $this->client_id,
497495
);
498496

497+
if ( $tenant_name ) {
498+
$args['tenant_name'] = $tenant_name;
499+
}
500+
501+
return $this->post( 'accounts/oauth_access_token', $args );
502+
499503
}
500504

501505
/**

tests/wpunit/APITest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,27 @@ public function testGetAccessTokenByAPIKeyAndSecretWithBlankClientID()
636636
$this->assertEquals($result->get_error_code(), $this->errorCode);
637637
}
638638

639+
/**
640+
* Test that fetching an Access Token using a tenant_name parameter returns the expected data.
641+
*
642+
* @since 2.0.7
643+
*/
644+
public function testGetAccessTokenByAPIKeyAndSecretWithTenantName()
645+
{
646+
$api = new ConvertKit_API_V4( $_ENV['CONVERTKIT_OAUTH_CLIENT_ID'], $_ENV['CONVERTKIT_OAUTH_REDIRECT_URI'] );
647+
$result = $api->get_access_token_by_api_key_and_secret(
648+
$_ENV['CONVERTKIT_API_KEY'],
649+
$_ENV['CONVERTKIT_API_SECRET'],
650+
'https://example.com'
651+
);
652+
$this->assertNotInstanceOf(WP_Error::class, $result);
653+
$this->assertIsArray($result);
654+
$this->assertArrayHasKey('oauth', $result);
655+
$this->assertArrayHasKey('access_token', $result['oauth']);
656+
$this->assertArrayHasKey('refresh_token', $result['oauth']);
657+
$this->assertArrayHasKey('expires_at', $result['oauth']);
658+
}
659+
639660
/**
640661
* Test that supplying valid API credentials to the API class returns the expected account information.
641662
*

0 commit comments

Comments
 (0)