Unable to get "Access token" Oauth2

I am trying to get an Access token to my Freeagent App but I am unable to
get it.

I am trying to follow this doc https://dev.freeagent.com/docs/oauth

1- The authorization request (successful)
2- The access token request (fail) [{result:{error:“invalid_grant”},“code”:400,“content_type”:“application/json;
charset=utf-8”}]

My php (symfony2) code:

/**

  • @Route("/admin/freeagent/config", name=“freeagent-config”)
    */
    public function freeagentConfigAction(Request $request)
    {
    //get these values from the FreeAgent developer dashboard
    $identifier = $this->getParameter(‘freeagent_connection’)[“client_id”];
    $secret = $this->getParameter(‘freeagent_connection’)[“client_secret”];

//the URL of this script. doesn’t have to be publicly accessible.
$script_url = $request->getUri();

//the base URL of the API. shouldn’t need to change this.
$base_url = ‘https://api.freeagent.com/v2’;

//create the OAuth client
$client = new OAuth2Client($identifier, $secret);

//check what stage we’re at
if (empty($_GET[‘code’]) && empty($_GET[‘token’])) {

//no code and no token so redirect user to FreeAgent to log in
$auth_url = $client->getAuthenticationUrl($base_url . '/approve_app', $script_url);
return $this->redirect($auth_url);

} elseif (isset($_GET[‘code’])) {

//we have a code so use it to get an access token
$response = $client->getAccessToken(
  $base_url . '/token_endpoint',
  'authorization_code',
  array('code' => $_GET['code'], 'redirect_uri' => $script_url)
);

if (isset($response['result']['access_token'])) {
  //normally you would store the token for use in future requests
  $token = $response['result']['access_token'];

  return $this->redirect($script_url . '?token=' . $token);
}
else {
  return new JsonResponse("Error getting Access Token".json_encode($response));
}

} elseif (isset($_GET[‘token’])) {

//when we have a token, just store the token.

$connection = $this->getParameter('freeagent_connection');

$connection["access_token"] = $_GET['token'];

$dumper = new Dumper();

$yaml = $dumper->dump(array(
  "parameters" => array (
    "freeagent_connection" => $connection
  )
));

$fileLocation = $this->kernel->getRootDir()."/../app/config/parameters-freeagent.yml";
file_put_contents($fileLocation, $yaml);


//show response
return new JsonResponse("Access token stored ".$_GET['token']);

}

}

I have fixed a bug in my code and it is working now.

The problem was this line. $script_url = $request->getUri();

getUri() using Symfony2 get the URLincluding the query string. The url
always have $GET[“code”].

Now is fixed using this to get the scrip url without the previous query
string.

$currentRoute = $request->attributes->get(‘_route’);
$script_url = $this->get(‘router’)->generate($currentRoute, array(), true);

My question i need always send the callback url the API. Is the API able to
get the URL where is the call and use this url as default?On Monday, 18 April 2016 11:53:31 UTC+1, ado...@symless.com wrote:

I am trying to get an Access token to my Freeagent App but I am unable to
get it.

I am trying to follow this doc FreeAgent Developer Dashboard

1- The authorization request (successful)
2- The access token request (fail) [{result:{error:“invalid_grant”},“code”:400,“content_type”:“application/json;
charset=utf-8”}]

My php (symfony2) code:

/**

  • @Route(“/admin/freeagent/config”, name=“freeagent-config”)
    */
    public function freeagentConfigAction(Request $request)
    {
    //get these values from the FreeAgent developer dashboard
    $identifier = $this->getParameter(‘freeagent_connection’)[“client_id”];
    $secret = $this->getParameter(‘freeagent_connection’)[“client_secret”];

//the URL of this script. doesn’t have to be publicly accessible.
$script_url = $request->getUri();

//the base URL of the API. shouldn’t need to change this.
$base_url = ‘https://api.freeagent.com/v2’;

//create the OAuth client
$client = new OAuth2Client($identifier, $secret);

//check what stage we’re at
if (empty($_GET[‘code’]) && empty($_GET[‘token’])) {

//no code and no token so redirect user to FreeAgent to log in
$auth_url = $client->getAuthenticationUrl($base_url . '/approve_app', $script_url);
return $this->redirect($auth_url);

} elseif (isset($_GET[‘code’])) {

//we have a code so use it to get an access token
$response = $client->getAccessToken(
  $base_url . '/token_endpoint',
  'authorization_code',
  array('code' => $_GET['code'], 'redirect_uri' => $script_url)
);

if (isset($response['result']['access_token'])) {
  //normally you would store the token for use in future requests
  $token = $response['result']['access_token'];

  return $this->redirect($script_url . '?token=' . $token);
}
else {
  return new JsonResponse("Error getting Access Token".json_encode($response));
}

} elseif (isset($_GET[‘token’])) {

//when we have a token, just store the token.

$connection = $this->getParameter('freeagent_connection');

$connection["access_token"] = $_GET['token'];

$dumper = new Dumper();

$yaml = $dumper->dump(array(
  "parameters" => array (
    "freeagent_connection" => $connection
  )
));

$fileLocation = $this->kernel->getRootDir()."/../app/config/parameters-freeagent.yml";
file_put_contents($fileLocation, $yaml);


//show response
return new JsonResponse("Access token stored ".$_GET['token']);

}

}