Getting invalid_grant for each authorization_code from the token endpoint. RRS feed

  • Question

  • We are developing a PHP web app which integrates with the Office365 Files API (or it should). I managed to get the Authorization Code from the authorization endpoint, but I cannot change it for Access Token. I am always getting invalid_grant, and it doesn't matter if I am just requested a new authorization_code.

    $clientSecret = $_ENV['AZURE_CLIENT_KEY'];
    $clientId = urlencode($_ENV['AZURE_CLIENT_ID']);
    $clientAuthCode = $_ENV['AZURE_AUTH_CODE'];
    $oauthUrl = $_ENV['AZURE_BASE_URL'] . 'token?api-version=1.0';
    $authReqBody = 'grant_type=authorization_code&client_id='.$clientId.'&'.'client_secret='.$clientSecret.'&'.'code='.$clientAuthCode .'&'.'redirect_uri='.urlencode('');
    $ch = curl_init();
    $chUrl = $oauthUrl;
    curl_setopt($ch, CURLOPT_ENCODING ,"");
    curl_setopt($ch, CURLOPT_URL, $chUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $authReqBody);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    return var_dump($output);

    Tuesday, January 27, 2015 9:22 AM