php创建按钮_使用PHP创建Microsoft登录按钮

php创建按钮

Creating a Microsoft Login Button using PHP

使用PHP创建Microsoft登录按钮

In this tutorial I will show you how to create a Microsoft login button for your website using PHP. To start with, let’s answer the question: What is OAuth? OAuth is a protocol used to allow secure authorization to websites and applications to access user information on other websites. There are two versions of OAuth, 1.0 and 2.0. In this post we will use OAuth 2.0 to build a Microsoft login system.

在本教程中,我将向您展示如何使用PHP为您的网站创建Microsoft登录按钮。 首先,让我们回答一个问题:什么是OAuth? OAuth是用于允许对网站和应用程序进行安全授权以访问其他网站上的用户信息的协议。 OAuth有两个版本:1.0和2.0。 在本文中,我们将使用OAuth 2.0构建Microsoft登录系统。

什么是Microsoft登录? (What is Microsoft Log-In?)

Microsoft Log-in means asking user to grant access to his/her Microsoft live information like email id, username etc. Once your website has been granted access and has all these information about the user it can allow the users to access protected pages on your website.

Microsoft登录是指要求用户授予其对他/她的Microsoft实时信息(如电子邮件ID,用户名等)的访问权限。一旦您的网站被授予访问权限并拥有有关该用户的所有这些信息,它便可以允许用户访问您网站上受保护的页面网站。

设置目录和文件 (Setting up Directory and Files)

Before we get started you need to create a PHP file named redirect.php. Place this file anywhere in your webspace.

在开始之前,您需要创建一个名为redirect.phpPHP文件。 将此文件放在您的网站空间中的任何位置。

创建一个Microsoft App (Creating a Microsoft App)

If your website is allowing login using Microsoft then your website is considered as an Microsoft app. So you have your website ready now its time to register you website as a Microsoft app. Follow this steps to create a Microsoft app:

如果您的网站允许使用Microsoft登录,则您的网站被视为Microsoft应用。 因此,您现在就可以准备好将网站注册为Microsoft应用程序的时候了。 请按照以下步骤创建Microsoft应用程序:

  1. Visit Microsoft apps page.

    访问Microsoft应用程序页面

  2. Now create a Microsoft app

    现在创建一个Microsoft应用
  3. Select API Settings and for redirect URL pass URL pointing to the redirect.php file.

    选择“ API设置”,然后为重定向URL传递指向redirect.php文件的URL。

  4. You can find the Client ID and Client Secret under App Settings

    您可以在“ 应用程序设置”下找到客户端ID和客户端密钥

使用Microsoft按钮创建登录 (Creating Login with Microsoft Button)

When user clicks on Login button you need to run this code to redirect user to Microsoft Live website so that user can grant permission to your app to access their information

当用户单击“登录”按钮时,您需要运行以下代码以将用户重定向到Microsoft Live网站,以便用户可以授予您的应用程序访问其信息的权限


$client_id = "";
$redirect_uri = "";
$scopes = "wl.basic,wl.offline_access,wl.signin,wl.emails";
header("Location: " . "https://login.live.com/oauth20_authorize.srf?client_id=" . $client_id . "&scope=" . $scopes . "&response_type=token&redirect_uri=" . $redirect_uri);

$client_id = "";
$redirect_uri = "";
$scopes = "wl.basic,wl.offline_access,wl.signin,wl.emails";
header("Location: " . "https://login.live.com/oauth20_authorize.srf?client_id=" . $client_id . "&scope=" . $scopes . "&response_type=token&redirect_uri=" . $redirect_uri);

Scopes represent the list of permissions for the app. You need to pass a comma separated list of permissions. List of all scopes.

范围代表应用程序的权限列表。 您需要传递以逗号分隔的权限列表。 所有作用域清单

Populate the $client_id and $redirect_uri variables.

填充$client_id$redirect_uri变量。

重定向回应用程序 (Redirecting back to the App)

Once user has given access to the app, Microsoft will redirect user back to the redirect URI. Now you need to retrieve an access token which acts like a permission to get user information.

一旦用户授予了对应用程序的访问权限,Microsoft将把用户重定向回重定向URI。 现在,您需要检索一个访问令牌,其作用类似于获得用户信息的权限。

In the redirect.php file you can retrieve access token by running this code

在redirect.php文件中,您可以通过运行以下代码来检索访问令牌


<?php
  $client_id = "";
  $client_secret = "";
  $redirect_uri = "";
  //$_GET["code"] is the authorization code
  if(isset($_GET["code"]))
  {
    //user granted permission
    //get access token using the authorization code
    $url = "https://login.live.com/oauth20_token.srf";
        $fields = array("client_id" => $client_id, "redirect_uri" => $redirect_uri, "client_secret" => $client_secret, "code" => $_GET["code"], "grant_type" => "authorization_code");
        foreach($fields as $key=>$value) { $fields_string .= $key."=".$value."&"; }
        rtrim($fields_string, "&");
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        $result = json_decode($result);
        curl_close($ch);
    //this is the refresh token used to access Microsoft Live REST APIs
        $access_token = $result->access_token;
        $refresh_token = $result->refresh_token;
  }
  else
  {
    echo "An error occured";
  }
?>

<?php
  $client_id = "";
  $client_secret = "";
  $redirect_uri = "";
  //$_GET["code"] is the authorization code
  if(isset($_GET["code"]))
  {
    //user granted permission
    //get access token using the authorization code
    $url = "https://login.live.com/oauth20_token.srf";
        $fields = array("client_id" => $client_id, "redirect_uri" => $redirect_uri, "client_secret" => $client_secret, "code" => $_GET["code"], "grant_type" => "authorization_code");
        foreach($fields as $key=>$value) { $fields_string .= $key."=".$value."&"; }
        rtrim($fields_string, "&");
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        $result = curl_exec($ch);
        $result = json_decode($result);
        curl_close($ch);
    //this is the refresh token used to access Microsoft Live REST APIs
        $access_token = $result->access_token;
        $refresh_token = $result->refresh_token;
  }
  else
  {
    echo "An error occured";
  }
?>

Populate variable $client_id,$client_secret and $redirect_uri.

填充变量$client_id$client_secret$redirect_uri

Finally we got $access_token and $refresh_token. $access_token usually expires in 1 hour therefore $refresh_token is used to get a new access token after every 1 hour.

最后,我们得到了$access_token$refresh_token$access_token通常在1小时后过期,因此$refresh_token每隔1小时就会用于获取新的访问令牌。

If access token is expired then you are likely to get an error in HTTP response content while making requests to REST APIs.

如果访问令牌已过期,则在向REST API发出请求时,您可能会在HTTP响应内容中收到错误消息。

You can retrieve new access token using this function

您可以使用此功能检索新的访问令牌


function new_access_token($refresh_token)
{
    $url = "https://login.live.com/oauth20_token.srf";
    $fields = array("client_id" => $client_id, "redirect_uri" => $redirect_uri, "client_secret" => $client_secret, "grant_type" => "refresh_token", "refresh_token" => $refresh_token);
    foreach($fields as $key=>$value) { $fields_string .= $key."=".$value."&"; }
    rtrim($fields_string, "&");
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    $result = json_decode($result);
    curl_close($ch);
    $access_token = $result->access_token;
    return $access_token;
}

function new_access_token($refresh_token)
{
    $url = "https://login.live.com/oauth20_token.srf";
    $fields = array("client_id" => $client_id, "redirect_uri" => $redirect_uri, "client_secret" => $client_secret, "grant_type" => "refresh_token", "refresh_token" => $refresh_token);
    foreach($fields as $key=>$value) { $fields_string .= $key."=".$value."&"; }
    rtrim($fields_string, "&");
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded"));
    curl_setopt($ch,CURLOPT_POST, count($fields));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    $result = json_decode($result);
    curl_close($ch);
    $access_token = $result->access_token;
    return $access_token;
}

调用REST API (Making calls to REST API)

You can find list of all REST APIs at Microsoft REST API reference. All the requests to these APIs must be made using the access token.

您可以在Microsoft REST API参考中找到所有REST API的列表。 对这些API的所有请求都必须使用访问令牌进行。

To retrieve user profile information you need to make a GET request of such kind

要检索用户个人资料信息,您需要发出此类GET请求


echo file_get_contents("https://apis.live.net/v5.0/me?access_token=" . $access_token);

echo file_get_contents("https://apis.live.net/v5.0/me?access_token=" . $access_token);

在WordPress中集成Microsoft登录 (Integrating Microsoft Login in WordPress)

WordPress is made on PHP therefore all code will be same for authorizing user and getting profile information. To create a redirect URL in WordPress use WordPress AJAX API.

WordPress是使用PHP制作的,因此用于授权用户和获取个人资料信息的所有代码都是相同的。 要在WordPress中创建重定向URL,请使用WordPress AJAX API

最后的想法 (Final Thoughts)

If you want to more than just Login then increase the permissions in permission list and store the access token and refresh token in database for further use. Make sure you update the access token when its refreshed. Don’t share the client secret with anyone.

如果您不仅要登录,还可以在权限列表中增加权限,并将访问令牌和刷新令牌存储在数据库中以备将来使用。 确保在刷新访问令牌后更新访问令牌。 不要与任何人共享客户机密。

翻译自: https://www.script-tutorials.com/creating-a-microsoft-login-button-using-php/

php创建按钮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值