/// <summary> /// 使用auth_code换取access_token及用户userId /// </summary> /// <param name="code"></param> /// <param name="redirectUri"></param> /// <returns></returns> protected override async Task <OAuthTokenResponse> ExchangeCodeAsync([NotNull] string code, [NotNull] string redirectUri) { try { var alipayRequest = new AlipaySystemOauthTokenRequest { Code = code, GrantType = "authorization_code" //GetApiName() }; var alipayResponse = await _alipayClient.ExecuteAsync(alipayRequest); if (alipayResponse.IsError) { Logger.LogError("An error occurred while retrieving an access token: the remote server " + "returned a {Status} response with the following payload: {Headers} {Body}.", /* Status: */ alipayResponse.Code, /* Headers: */ alipayResponse.Msg, /* Body: */ alipayResponse.Body); return(OAuthTokenResponse.Failed(new Exception("An error occurred while retrieving an access token."))); } else { var payload = JObject.FromObject(alipayResponse); return(OAuthTokenResponse.Success(payload)); } } catch (Exception ex) { Logger.LogError(ex.ToString()); return(OAuthTokenResponse.Failed(new Exception("An error occurred while retrieving an access token."))); } }
public async Task ImageUpload() { var client = new DefaultAopClient("https://openapi.alipaydev.com/gateway.do", "", "", "json", "1.0", "RSA2", "", "UTF-8", false); AlipayOfflineMaterialImageUploadRequest request = new AlipayOfflineMaterialImageUploadRequest(); request.ImageType = "jpg"; request.ImageName = "logo图片"; FileItem imageContent = new FileItem("edgeless.jpg"); request.ImageContent = imageContent; request.ImagePid = "201901101551"; AlipayOfflineMaterialImageUploadResponse response = await client.ExecuteAsync(request); }
/// <summary> /// 获取用户的信息 /// </summary> /// <param name="token"></param> /// <param name="code"></param> /// <returns></returns> public async Task <MyJsonResult> GetUserInfoAsync(string token) { //构建公共参数 IAopClient client = new DefaultAopClient(ServerUrl, Appid, PriKey, "json", "1.0", "RSA2", PubKey, null, false); AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest(); //执行 AlipayUserInfoShareResponse response = await client.ExecuteAsync(request, token); //构建返回的结果 if (response.Code != "10000") { myJsonResult.code = (int)MyJsonResultEnum.thirdError; myJsonResult.failMsg = response.SubMsg; } myJsonResult.rows = response; return(myJsonResult); }
/// <summary> /// 获取授权token等信息 /// </summary> /// <param name="code"></param> /// <returns></returns> public async Task <MyJsonResult> AccessTokenAsync(string code) { //定义一个响应的信息 var res = ""; IAopClient client = new DefaultAopClient(ServerUrl, Appid, PriKey, "json", "1.0", "RSA2", PubKey, null, false); AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest(); request.GrantType = "authorization_code"; request.Code = code; //request.RefreshToken = "201208134b203fe6c11548bcabd8da5bb087a83b"; AlipaySystemOauthTokenResponse response = await client.ExecuteAsync(request); if (response.AccessToken.IsNullOrEmpty()) { myJsonResult.code = (int)MyJsonResultEnum.thirdError; myJsonResult.failMsg = response.SubMsg; return(myJsonResult); } res = response.ToJson(); myJsonResult.rows = res; return(myJsonResult); }