zoukankan      html  css  js  c++  java
  • 支付宝生活号授权获取用户信息

    1.第一步在蚂蚁开放平台注册申请应用上线

    2.下载支付宝秘钥生成器,生成秘钥(加在代码里面)和公钥(填写到蚂蚁开放平台)下载sdk放到extend文件夹里面

    3.引用SDK

    //头部加上
    use thinkLoader;
    header("Content-type:text/html;charset=utf-8");
    Loader::import('alipay.AopSdk', EXTEND_PATH);

    4.访问方法拼写授权地址

    $encoded_url = 回调地址;
    $appid = APPID;
    $code_url = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=$appid&scope=auth_user&redirect_uri=".urlencode($encoded_url);//获取auth_code
    $this->redirect($code_url);

    5.获取用户信息

    $code = $_GET['auth_code'];
            //APPID
            $appid = '支付宝平台的appid';
            //应用私钥  文件名(rsa_private_key.pem)
            $rsaPrivateKey = "应用秘钥";
            //支付宝公钥  文件名 (rsa_public_key.pem)
            $alipayrsaPublicKey = "支付宝公钥";
            //初始化
            $aop = new AopClient ();
            $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
            $aop->appId = $appid;
            $aop->rsaPrivateKey = $rsaPrivateKey;
            $aop->alipayrsaPublicKey = $alipayrsaPublicKey;
            $aop->apiVersion = '1.0';
            $aop->signType = 'RSA2';
            $aop->postCharset='UTF-8';
            $aop->format='json';
            //获取access_token
            $request = new AlipaySystemOauthTokenRequest();
            $request->setGrantType("authorization_code");
            $request->setCode($code);
            $result = $aop->execute($request);
            $access_token = $result->alipay_system_oauth_token_response->access_token;
            //获取用户信息
            $request_a = new AlipayUserinfoShareRequest();
            $result_a = $aop->execute ($request_a,'authusrB4d426f65fc0f4a1299ac3e6cc48ffX00');
            //这里传入获取的access_token
            $responseNode_a = str_replace(".", "_", $request_a->getApiMethodName()) . "_response";
            $user_id = $result_a->$responseNode_a->user_id;
            //用户唯一id
            $headimgurl = $result_a->$responseNode_a->avatar;
            //用户头像
            $nick_name = $result_a->$responseNode_a->nick_name;
            //用户昵称
            var_dump($user_id);
            var_dump($headimgurl);
            var_dump($nick_name);
            var_dump($result_a->$responseNode_a);
            die;
  • 相关阅读:
    注解
    使用反射机制调用属性和私有成员与代理模式的介绍
    动态代理模式
    SVN的安装与常用功能使用以及解决安装配置过程中的一些错误
    企业私服
    -Java-log4j
    List,Set,Map用法以及区别
    接口和抽象类有什么区别
    Dalvik opcodes
    外派公司或者外包公司,真的适合选择吗?
  • 原文地址:https://www.cnblogs.com/notesbooks/p/9728966.html
Copyright © 2011-2022 走看看