Showing posts with label office 365. Show all posts
Showing posts with label office 365. Show all posts

Tuesday, January 3

office 365 token – How to get token in office 365 using PHP?

 office 365 token – How to get token in office 365 using PHP?

How to get a refresh office 365 token and office 365 access token in office 365 using PHP

In this post we will show you how to get office 365 token and in office 365 using php. after completed process of “Registering your app for OneDrive API” you need add this code in php file.
In this following code you have to pass CLIENT ID , CLIENT SECRET and REDIRECT URL. By passing this code, 1st it will ask login and after login, it will print refresh token and access token and other information of office 365.
Reference of office 365 Api
In this code we pass in 3 stage ::
1. Log-in and get an authorization code
2. Redeem the authorization code for tokens
3. Discover the OneDrive for Business resource URI

// ADD YOUR CLIENT ID
$client_id     = "ADD-YOUR-CLIENT-ID";
// ADD YOUR CLIENT SECRET
$client_secret = "ADD-YOUR-CLIENT-SECRET";
// ADD REDIRECT URL
$redirect_uri  = "HTTPS://YOUR-DOMIN.COM/FOLDER-NAME/index.php";

echo "<h1>office 365 api test</h1>";
$_response = "https://login.microsoftonline.com/common/oauth2/authorize?client_id=".$client_id."&scope=wl.signin%20wl.offline_access%20wl.skydrive_update%20wl.basic&response_type=code&redirect_uri=".urlencode($redirect_uri);   

if(!isset($_GET['code']))
{
    // you need to login hear to get code and token
    // Log-in and get an authorization code
    echo "<h2>LOGIN</h2>";
    echo "<span style='vertical-align: middle;'><a href='".$_response."'>Login Hear</a></span>";
}
else
{   
    //  Redeem the authorization code for tokens
    // get code usnig $_GET method
    $auth = $_GET['code'];
    $arraytoreturn = array();
    $output = "";
   
    $resource_id = "https://api.office.com/discovery/";
    // get token form office api
    // Redeem the authorization code for tokens
    try {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://login.microsoftonline.com/common/oauth2/token");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);   
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/x-www-form-urlencoded',
        ));
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);       

        $data = "client_id=".$client_id."&redirect_uri=".urlencode($redirect_uri)."&client_secret=".urlencode($client_secret)."&code=".$auth."&grant_type=authorization_code&resource=".$resource_id;   
       
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
    }
    catch (Exception $e)
    {
        var_dump($e);
    }

    $out2 = json_decode($output, true);
    $get_access_token = $out2['access_token'];
       
    echo "Access Token is : ".$get_access_token."<br>";   
    echo "<pre>";
    print_r($out2);   
    echo "</pre>";
   
    // Discover the OneDrive for Business resource URI
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.office.com/discovery/v1.0/me/services');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $headers[0] = 'Authorization: Bearer ' . $get_access_token;
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($ch);
    curl_close($ch);
   
    $get_auto_result = json_decode($response, true);
    $get_auto_result_value = $get_auto_result['value'];
   
    // print Business resource URI
    echo "<pre>";
    print_r($get_auto_result_value);
    echo "</pre>";   
}

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...