user2993349
user2993349

Reputation: 501

Outlook API Refresh Token

I'm using the simple-oauth2 library to get the token, but it only has the acess token and the id token.

How do I get the refresh token?

Upvotes: 0

Views: 2577

Answers (3)

Soma Yarlagadda
Soma Yarlagadda

Reputation: 2943

The Refresh tokens are issued by the authorization server and it is up to the authorization server's discretion to issue them. Since you mentioned Outlook API you could you Microsoft Graph API for refresh token.

Please look at these links: Outlook Dev Center, Microsoft Graph API for details on how to get a refresh token for Outlook API.

Upvotes: 0

Virendra Kumar
Virendra Kumar

Reputation: 21

function refresh_token($token)
{
  // Build the form data to post to the OAuth2 token endpoint
  $token_request_data = array(
    "grant_type" => 'refresh_token',
    "refresh_token" => $token,
    "redirect_uri" => 'https://login.microsoftonline.com/common/oauth2/token',
    "resource" => "https://outlook.office365.com/",
    "client_id" => self::$clientId,
    "client_secret" => self::$clientSecret
  );

  // Calling http_build_query is important to get the data
  // formatted as Azure expects.
  $token_request_body = http_build_query($token_request_data);
  error_log("Request body: ".$token_request_body);

  $curl = curl_init(self::$authority.self::$tokenUrl);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($curl, CURLOPT_POST, true);
  curl_setopt($curl, CURLOPT_POSTFIELDS, $token_request_body);
  /*custom*/
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  /*custom end*/
  $response = curl_exec($curl);
  error_log("curl_exec done.");

  $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  error_log("Request returned status ".$httpCode);
  if ($httpCode >= 400) {
    return array('errorNumber' => $httpCode,
                 'error' => 'Token request returned HTTP error '.$httpCode);
  }

  // Check error
  $curl_errno = curl_errno($curl);
  $curl_err = curl_error($curl);
  if ($curl_errno) {
    $msg = $curl_errno.": ".$curl_err;
    error_log("CURL returned an error: ".$msg);
    return array('errorNumber' => $curl_errno,
                 'error' => $msg);
  }

  curl_close($curl);

  // The response is a JSON payload, so decode it into
  // an array.
  $json_vals = json_decode($response, true);
  error_log("TOKEN RESPONSE:");
  foreach ($json_vals as $key=>$value) {
    error_log("  ".$key.": ".$value);
  }

  return $json_vals;
}

Upvotes: 0

Ujwal Baidar
Ujwal Baidar

Reputation: 77

I had same problem. you need to add scope parameter ['offline_access'] in authUrl add:

 {access_type:'offline', approval_prompt: 'force'}

This should give you refresh token along auth_tokens and id_token

Upvotes: 2

Related Questions