1、下载ServiceWindow_Demo_DotNet :引用alipay-sdk-NET

2、

 #region GetUserID(string ALIPAY_GATEWAY, string auth_code, string APP_ID, string PRIVATE_KEY, string ALIPAY_PUBLIC_KEY, out string access_token, out string alipay_user_id)
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ALIPAY_GATEWAY"></param>
    /// <param name="auth_code"></param>
    /// <param name="APP_ID"></param>
    /// <param name="PRIVATE_KEY"></param>
    /// <param name="ALIPAY_PUBLIC_KEY"></param>
    /// <param name="access_token"></param>
    /// <param name="alipay_user_id"></param>
    /// <returns></returns>
    public static string GetUserID(string ALIPAY_GATEWAY, string auth_code, string APP_ID, string PRIVATE_KEY, string ALIPAY_PUBLIC_KEY, out string access_token, out string alipay_user_id)
    {
        string result = "", straccess_token = "", stralipay_user_id="";

        try
        {
            
           IAopClient client = new DefaultAopClient(ALIPAY_GATEWAY, APP_ID, PRIVATE_KEY, "json", "1.0", "RSA2", ALIPAY_PUBLIC_KEY, "utf-8", false);

            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.GrantType = "authorization_code";
            request.Code = auth_code;
            request.RefreshToken = "";
            AlipaySystemOauthTokenResponse response = client.Execute(request);
            JObject jobj = (JObject)JsonConvert.DeserializeObject(response.Body);

            result = jobj.ToString();
            JObject jk = (JObject)jobj["alipay_system_oauth_token_response"];
            if (jk.Property("code") == null)
            {
                straccess_token = jobj["alipay_system_oauth_token_response"]["access_token"].ToString();
                stralipay_user_id = jobj["alipay_system_oauth_token_response"]["alipay_user_id"].ToString();
                string user_id = jobj["alipay_system_oauth_token_response"]["user_id"].ToString();
                
                result = user_id;
            }
            else
            {
                string msg = jobj["alipay_system_oauth_token_response"]["msg"].ToString();
                result = msg;
            }
        }
        catch (Exception ex)
        {
            result = "异常错误:【" + ex.Message + "】";
        }
        access_token = straccess_token;
        alipay_user_id = stralipay_user_id;
        return result;
    }

 
    #endregion

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐