调用集成代码前,请先查看服务商代商户获取用户信息流程说明:[url]https://openclub.alipay.com/read.php?tid=12762&fid=68[/url]
使用沙箱模拟服务商代商户获取用户信息逻辑流程如下:
服务商appid:2016101800718925;
商户appid:2016091500520089;pid:2088102175800172;
用户pid:2088102175794899;
1、第三方应用授权
(1)第三方应用授权链接
[url]https://openauth.alipaydev.com/oauth2/appToAppAuth.htm?app_id=2016101800718925&redirect_uri=https%3a%2f%2fwww.baidu.com%2f[/url]
注:沙箱环境测试拼接链接头部与正式环境不同:[url]https://openauth.alipay[/url]dev.com/oauth2/appToAppAuth.htm?app_id=APPID&redirect_uri=ENCODED_URL
(2)获取app_auth_code
[url]https://www.baidu.com/?app_id=2016101800718925&source=alipay_app_auth&app_auth_code=8e5e571d6ca74809ae23ba2669af6X17[/url]
(3)换取应用授权令牌
public static void main(String[] args) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); AlipayOpenAuthTokenAppRequest request = new AlipayOpenAuthTokenAppRequest(); //授权成功后,使用app_auth_code换取app_auth_token AlipayOpenAuthTokenAppModel model = new AlipayOpenAuthTokenAppModel(); //换取令牌,不改 model.setGrantType("authorization_code"); //app_auth_code参数 model.setCode("b47d70d0b43d4a46ae23c438f60afX17"); /*//刷新令牌 model.setRefreshToken("a3dcea3a35a342858391dfeaa336cE75");*/ request.setBizModel(model); try { AlipayOpenAuthTokenAppResponse response = alipayClient.execute(request); System.out.println(response.getBody()); } catch (AlipayApiException e) { //处理异常 e.printStackTrace(); } }
响应参数:
{"alipay_open_auth_token_app_response":{"code":"10000","msg":"Success","app_auth_token":"201901BB8b1c906179484ce893077fd696f4bC17","app_refresh_token":"201901BB5547a7886d7f4ac68605fa493677eC17","auth_app_id":"2016091500520089","expires_in":31536000,"re_expires_in":32140800,"tokens":[{"app_auth_token":"201901BB8b1c906179484ce893077fd696f4bC17","app_refresh_token":"201901BB5547a7886d7f4ac68605fa493677eC17","auth_app_id":"2016091500520089","expires_in":31536000,"re_expires_in":32140800,"user_id":"2088102175800172"}],"user_id":"2088102175800172"},"sign":"KRBeKgLQo8SvL/oGiybhJzh5CZjmF9KeGXlQj/pHUTtVC8cjZA/Jm6sGn0K3LNpmZ38YJOocuvKHqkoeU2SAMLYkreRBjWP5cjqWNsVScQtBh1Kwe3ePSoN7XIkyxHg9G3tqdMlk/ZEffYNVvin94KH8HJ2pBQljanM8l/VjueAmX1EdPPMOHafvWdx4hE6rz4PUy4/s+6nQADOUzP0ctW51CTk2A/e3qIYlf3kL6w8J3K2ohPCOBhPDypUnm5CSEPHCrdFHeJVw3fLPSsAqtfIqjWBIWdDzUl2ogHBf16pEiUpUtiEoDb/p1X3fxQCRrJ87XUqk5YW9EOVltx5zgA=="}
2、用户信息授权
(1)用户信息授权链接
[url]https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm?app_id=2016091500520089&scope=auth_user[/url],auth_base&redirect_uri=https%3a%2f%2fwww.baidu.com%2f
注:沙箱环境测试拼接链接头部与正式环境不同:[url]https://openauth.alipay[/url]dev.com/oauth2/publicAppAuthorize.htm?app_id=APPID&scope=SCOPE&redirect_uri=ENCODED_URL
(2)获取auth_code
[url]https://www.baidu.com/?app_id=2016091500520089&source=alipay_wallet&scope=auth_base[/url],auth_user&auth_code=1f80c8d6c21b4a8a94345e07d823QB89
(3)换取授权访问令牌
public static void main(String[] args) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); /** * 用户授权没有AlipaySystemOauthTokenModel,直接使用request */ //值为authorization_code时,代表用code换取;值为refresh_token时,代表用refresh_token换取 request.setGrantType("authorization_code"); //授权码,用户对应用授权后得到。即下面获取到的AccessToken //每次用户授权完成,回调地址中的auth_code将不一样,auth_code只能使用一次,一天未被使用自动过期。 request.setCode("1f80c8d6c21b4a8a94345e07d823QB89"); //刷新令牌,上次换取访问令牌时得到 //request.setRefreshToken("43e3bee07f354cc5a7780446412bSX75"); try { String app_auth_token="201901BB8b1c906179484ce893077fd696f4bC17"; //自调用 // AlipaySystemOauthTokenResponse response = alipayClient.execute(request); //第三方调用接口 AlipaySystemOauthTokenResponse response = alipayClient.execute(request,null,app_auth_token); System.out.println(response.getBody()); } catch (AlipayApiException e) { //处理异常 e.printStackTrace(); } }
响应参数:
{"alipay_system_oauth_token_response":{"access_token":"composeBd364827b102144cf992fb7ce6e327X89","alipay_user_id":"2088102175794899","expires_in":600,"re_expires_in":660,"refresh_token":"composeBdefde11796f24938bafebefcf7834D89","user_id":"2088102175794899"},"sign":"NtsgUCZg04C5DdRnBdFSyFgg8K5pcFSnGbGV67zpo6tAuDEUB6421lAm2mdG8UHdO1PlLoSdSZmXgNZgin3bkjFe4hyOQUroPLb64whTCas5BsKBODL2s0+8LlrGnF7VJNkOSaWrDIh15DFNSKLvtA3gtvo3gvZK5gbY/428HzEE8HpBC85CEZuAA+0RsVXJvk/lvcXtAfjEKs5c1s45aOq8UmcH+HjAjmcPptNHokxYF+iY++xRgaypz7PeHMJsJNI8PS0CvznYySXwvHi9z/Ooav79X5gjjftqpd/2aSeUljbXdlf66LhcBaAFj8eT1r/Zkz+xQ2jE5M8i95HniQ=="}
3、获取会员信息
获取会员信息接口
public static void main(String[] args) throws AlipayApiException { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //调用接口获取用户信息 AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); //需要用户授权后,将auth_code换取accessToken,注入 String accessToken = "composeBd364827b102144cf992fb7ce6e327X89"; try { String app_auth_token="201901BB8b1c906179484ce893077fd696f4bC17"; //第三方调用接口 AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken,app_auth_token); //自调用 // AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken); System.out.println(response.getBody()); } catch (AlipayApiException e) { //处理异常 e.printStackTrace(); } }
响应参数:
{"alipay_user_info_share_response":{"code":"10000","msg":"Success","gender":"f","is_certified":"T","is_student_certified":"F","nick_name":"买家沙箱","user_id":"2088102175794899","user_status":"T","user_type":"2"},"sign":"GFOQ5SbhPPSkL1Dmf9snUJxTEeCxngsXOQubURBIDx5C9/sb7CnNmHdSXDru9NPDlvfSQPT+b+iUOVapARafpsdqP6XIdHqQfrLB2jE6YO2ql52J07/yGrUNAwjaaKd/ndBbpljhn9laQATD1sqrwwUGmttiGkyK6cPvW3lH/ndAvbDRkwnKdrLKZGSou3erf0thL6QzlS12XXryTnvEArCuteAsqqDFi81R/PVKyXdl15P9SdVQzhJski5yZ3A1VEO9Vv96bVjVlgUZaIALddjOX4rh100e/mxx7K8qOmjX12r72pFyPrVEAQChRTSx1OxiXureiCScscHKtZdOQg=="}
设置app_auth_token和accessToken的代码方式:
注:accessToken没有可以设置为null。
PHP:
$result = $aop->execute($request,$accessToken,$app_auth_token);
.net:
response=client.execute(request,accessToken,app_auth_token);
如果还是报错,请按如下方式进行提问:
正确提问方式:使用环境(沙箱还是正式环境)+接口名称(中文+英文)+ 请求参数 + 报错信息详情