说明:
本帖是利用支付宝沙箱测试获取会员信息接口,demo中会配置个人的沙箱账号信息,方便大家测试。
测试环境:JAVA1.6+,eclipse
接口文档:【查看】
沙箱环境测试改正式环境测试请修改网关为下方值
正式环境网关:[url]https://openapi.alipay.com/gateway.do[/url]
demo示例代码下载:download:获取会员信息demo.zip
测试流程(声明:demo仅做参考使用):
1.生成应用,配置密钥可参考该文档:[url]https://read.php?tid=1606&fid=25[/url]
2.正式环境URL拼接规则:[url]https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=APPID&scope=SCOPE&redirect_uri=ENCODED_URL[/url]
a). redirect_uri :在你创建的应用中进行查看,授权回调地址是自己配置自己所想要访问的地址,
将授权回调地址encode方能进行使用;
如:
授权回调地址为:[url]https://www.baidu.com/[/url]
则redirect_uri内容为:https%3A%2F%2Fwww.baidu.com%2F
b).关于scope的说明可参考该贴:[url]https://read.php?tid=1795&fid=43[/url]
以下是我以沙箱为例拼接的url:
[url]https://openauth.alipaydev.com/oauth2/publicAppAuthorize.htm?app_id=2016101800718925&scope=auth_user[/url],auth_base&redirect_uri=https%3A%2F%2Fwww.baidu.com%2F
3.将拼接好的url进行访问授权获取auth_code
注:每次用户授权完成,回调地址中的auth_code将不一样,auth_code只能使用一次,一天未被使用自动过期。
4.将auth_code放入code中,使用auth_code换取接口access_token及用户userId
package com.alipay.demo; import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.request.AlipaySystemOauthTokenRequest;import com.alipay.api.response.AlipaySystemOauthTokenResponse;import com.alipay.config.AlipayConfig; /** * alipay.system.oauth.token(换取授权访问令牌) * 参考文档:[url]https://docs.open.alipay.com/api_9/alipay.system.oauth.token[/url] * @author wb-wly251833 * */public class alipaySystemOauthToken { 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(); //值为authorization_code时,代表用code换取;值为refresh_token时,代表用refresh_token换取 request.setGrantType("authorization_code"); //授权码,用户对应用授权后得到。 request.setCode("b5300073ee7c4dae8b00e060d161UC75"); //刷新令牌,上次换取访问令牌时得到,即:AccessToken //request.setRefreshToken("43e3bee07f354cc5a7780446412bSX75"); try { AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request); //访问令牌。通过该令牌调用需要授权类接口 System.out.println("访问令牌AccessToken:"+oauthTokenResponse.getAccessToken()); System.out.println(oauthTokenResponse.getBody()); } catch (AlipayApiException e) { //处理异常 e.printStackTrace(); } }}
运行结果:
访问令牌AccessToken:composeB9de83fb7e93f4c5ea985113c774d5X75{"alipay_system_oauth_token_response":{"access_token":"composeB9de83fb7e93f4c5ea985113c774d5X75","alipay_user_id":"2088102169462753","expires_in":600,"re_expires_in":660,"refresh_token":"composeBab117bfd2a85430d8f08906843d97X75","user_id":"2088102169462753"},"sign":"Rz9D2c0frNntC7mORRlJUHXC2p5m0gGHKVRxbsbcfH/kyma14GflU6qkSXfBw0I3pkukCMNUwJ2tU1ZnLilBescQC9W0JCwU13e8aORCesljVk70NdGzCX9Ui9avMQYaVAsqqMq5iUiZjy5u4Y8dW0Of9ldfyLeDI6PxyHZvhQK86U82B86mS81/TnitgVDyiCUlHjYtySfNruljInb3BJb8ejm/nW1tF7F2ZeAb/eKiv4YwiI72znyIA5WIqeyvSLqUA10xrOPjgWCO4rL/bWDcOSU4zqjQ8HsBvOLBfbwzA7V4Q2fM4Em17Kn8D6jIP+DzkHXKfuNAfFXsPSDrcA=="}
cope=auth_base,到这一步就可以获取到用户的userId