Skip to content

Commit

Permalink
Merge pull request #1 from Aracon/Aracon-patch-1
Browse files Browse the repository at this point in the history
Added getFirstAccessToken() method
  • Loading branch information
Aracon committed Jun 13, 2015
2 parents 93a4139 + ffe6951 commit db1684f
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/bitrix24.php
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ public function getNewAccessToken()
{
throw new Bitrix24Exception('application id not found, you must call setRefreshToken method before');
}
elseif(is_null($applicationScope))
elseif(empty($applicationScope))
{
throw new Bitrix24Exception('application scope not found, you must call setApplicationScope method before');
}
Expand All @@ -598,6 +598,55 @@ public function getNewAccessToken()
return $requestResult;
}

/**
* Authorize and get first access token
* @return array
* @throws Bitrix24Exception
*/
public function getFirstAccessToken($code)
{
$domain = $this->getDomain();
$applicationId = $this->getApplicationId();
$applicationSecret = $this->getApplicationSecret();
//$refreshToken = $this->getRefreshToken();
$applicationScope = $this->getApplicationScope();
$redirectUri = $this->getRedirectUri();

if(is_null($domain))
{
throw new Bitrix24Exception('domain not found, you must call setDomain method before');
}
elseif(is_null($applicationId))
{
throw new Bitrix24Exception('application id not found, you must call setApplicationId method before');
}
elseif(is_null($applicationSecret))
{
throw new Bitrix24Exception('application id not found, you must call setApplicationSecret method before');
}
elseif(empty($applicationScope))
{
throw new Bitrix24Exception('application scope not found, you must call setApplicationScope method before');
}
elseif(is_null($redirectUri))
{
throw new Bitrix24Exception('application redirect URI not found, you must call setRedirectUri method before');
}

$url = 'https://'.$domain."/oauth/token/".
"?client_id=".urlencode($applicationId).
"&grant_type=authorization_code".
"&client_secret=".$applicationSecret.
'&scope='.implode(',', array_map('urlencode', array_unique($applicationScope))).
'&redirect_uri='.urlencode($redirectUri).
'&code='.urlencode($_GET['code']);

$requestResult = $this->executeRequest($url);
// handling bitrix24 api-level errors
$this->handleBitrix24APILevelErrors($requestResult, 'get first access token');
return $requestResult;
}

/**
* Сheck is access token expire, сall list of all available api-methods from B24 portal with current access token
* if we have an error code expired_token then return true else return false
Expand Down Expand Up @@ -695,4 +744,4 @@ public function getScope($isFull=false)
$requestResult = $this->executeRequest($url);
return $requestResult;
}
}
}

0 comments on commit db1684f

Please sign in to comment.