获取eaby token

    /**
     * @title 获取eaby token
     * @author 卡卡
     */
    public function get_eaby_token()
    {

        $data = input('');
        $store = Db::name('store');
        $where['status'] = 1;
        $where['name'] = $data['name'];
        $code = $data['code'];
        $result = $store->where($where)->find();
        if (empty($result)) {
            $msg['message'] = "未找到该店铺信息";
            return errorJson($msg);
        } else {
            # 正式数据
            $clientId = $result['seller_id'];
            $clientSecret = $result['seller_secret'];
            $redirect_uri = $result['seller_url'];
            $credentials = base64_encode($clientId . ':' . $clientSecret);
            // 第二步:用授权代码交换用户token (User Token)和更新token (refresh_token)
            $postdata = http_build_query([
                'grant_type' => 'authorization_code',
                'code' => $code,
                'redirect_uri' => $redirect_uri
            ]);
            $options = array(
                'http' => array(
                    'method' => 'POST',
                    'ignore_errors' => true,
                    'header' => "Host:api.sandbox.ebay.com\r\n" . "Content-type:application/x-www-form-urlencoded\r\n" . "Authorization:Basic " . $credentials . "\r\n",
                    'content' => $postdata,
                    'timeout' => 60, // 超时时间(单位:s)
                )
            );
            $context = stream_context_create($options);
            $response = file_get_contents("https://api.ebay.com/identity/v1/oauth2/token", TRUE, $context);
            $response = json_decode($response);
            $response = object_array($response);
            // print_r($response);
            $msg['response'] = $response;

            if (isset($response['error'])) {
                $msg['error'] = $response['error'];
                $msg['error_description'] = $response['error_description'];
                return errorJson($msg);
            } else {
                $save['access_token'] = $response['access_token'];
                $save['refresh_token'] = $response['refresh_token'];
                $save['expires_in'] = $response['expires_in'];
                $save['refresh_token_expires_in'] = $response['refresh_token_expires_in'];
                $save['update_time'] = time();
                $result_update = $store->where($where)->update($save);
                if ($result_update) {
                    $msg['message'] = "授权成功";
                    return successJson($msg);
                } else {
                    $msg['message'] = "授权失败";
                    return errorJson($msg);
                }
            }
        }
    }


/**
 *解析 JSON 对象
 */
function object_array($array)
{
    if (is_object($array)) {
        $array = (array)$array;
    }
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            $array[$key] = object_array($value);
        }
    }
    return $array;
}

 

posted @ 2021-04-16 19:47  爱家家的卡卡  阅读(97)  评论(0编辑  收藏  举报