PHP 饿了么授权
程序员文章站
2022-03-26 11:37:11
...
PHP 饿了么授权
饿了么商家开放平台:https://open.shop.ele.me/openapi/account
文档中心:https://open.shop.ele.me/openapi/documents/joinup
授权文档:https://open.shop.ele.me/openapi/documents/isvoauth
第一步:
拼接链接
$app_key = ""; //
$app_secret = ""; //
$state="1";
$scope = "all";
$callback_url = ""; //应用的授权回调地址
$url = "https://open-api.shop.ele.me/authorize?response_type=code&client_id=$app_key&state=$state&redirect_uri=$callback_url&scope=$scope";
通过地址访问 进行授权
输入账号密码去获取 code
获取到 code
获取code 并进行获取 access_token
返回的参数 都保存到数据库
//SDk 文件引入
include 'ElemeOpenApi\Config\Config.php';
include 'ElemeOpenApi\OAuth\OAuthClient.php';
$code = $_GET["code"];
$state="1";
$scope = "all";
$callback_url = ""; //回调地址
//实例化一个配置类
$config = new \ElemeOpenApi\Config\Config($app_key, $app_secret, false);
//使用config对象,实例化一个授权类
$client = new \ElemeOpenApi\OAuth\OAuthClient($config);
$token = $client->get_token_by_code($code, $callback_url);
//取值
$access_token = $token->access_token;
$token_type = $token->token_type;
$expires_in = $token->expires_in;
$refresh_token = $token->refresh_token;
SDK 下载:https://open.shop.ele.me/openapi/documents/sdk 下载PHP
token 失效
刷新 token 需要用到 刚刚上一步授权 返回的 refresh_token
$app_key = ""; //
$app_secret = ""; //
//数据库里面查询的
$refresh_token = $_POST["refresh_token"];
$scope = $_POST["scope"];
//实例化一个配置类
$config = new \ElemeOpenApi\Config\Config($app_key, $app_secret, false);
//使用config对象,实例化一个授权类
$client = new \ElemeOpenApi\OAuth\OAuthClient($config);
//根据OAuth2.0中的对应state,scope和callback_url,获取授权URL
$token = $client->get_token_by_refresh_token($refresh_token,$scope);
$access_token = $token->access_token;
$token_type = $token->token_type;
$expires_in = $token->expires_in;
$refresh_tokens = $token->refresh_token;
更新数据库 保存下来