コード例 #1
0
        /// <summary>
        /// 获取AccessToken
        ///
        /// 范例
        /// https://open.1688.com/api/sysAuth.htm?spm=a260s.8208014.0.0.iacZUD&aamp;ns=cn.alibaba.open
        /// </summary>
        /// <param name="clientId">appKey</param>
        /// <param name="clientSecret">app密钥</param>
        /// <param name="preAuthCode">临时令牌 code</param>
        /// <param name="apiRoot">api 目录 </param>
        /// <param name="apiVersion">api 版本号</param>
        /// <param name="apiNamespace">api 命名空间</param>
        /// <param name="apiName">api 名称</param>
        /// <param name="protocol">调用协议</param>
        /// <param name="redirectUrl">回调地址</param>
        /// <param name="grantType">授权类型</param>
        /// <param name="needRefreshToken">是否需要刷新token</param>
        /// <param name="sessionType">请求环境</param>
        /// <param name="requestSchema">请求协议</param>
        /// <returns></returns>
        public static OAuthAccessTokenResult GetAccessToken(
            string clientId,
            string clientSecret,
            string preAuthCode,
            string apiRoot                = "openapi",
            string apiVersion             = "1",
            string apiNamespace           = "system.oauth2",
            string apiName                = "getToken",
            Com.Alibaba.Protocol protocol = Com.Alibaba.Protocol.http,
            string redirectUrl            = "http://localhost:12305",
            string grantType              = "authorization_code",
            bool needRefreshToken         = true,
            SessionType sessionType       = SessionType.Prod,
            UriSchema requestSchema       = UriSchema.https)
        {
            var url = new StringBuilder();

            url.Append(sessionType == SessionType.Sandbox ? Com.Alibaba.Config.RequestUriRootSandbox.UriSchemaFill(requestSchema) : Com.Alibaba.Config.RequestUriRoot.UriSchemaFill(requestSchema));
            url.Append($"/{apiRoot}/{protocol.ToString()}/{apiVersion}/{apiNamespace}/{apiName}/{clientId}");

            var data = new
            {
                grant_type         = grantType,
                need_refresh_token = needRefreshToken.ToString(),
                client_id          = clientId,     //   Your app's client ID
                client_secret      = clientSecret, //   Your app's client secret
                redirect_uri       = redirectUrl,
                code = preAuthCode                 //    The authorization code you received
            };

            return(CommonJsonSend.Send <OAuthAccessTokenResult>(null, url.ToString(), data, CommonJsonSendType.POST));
        }
コード例 #2
0
        /// <summary>
        /// Obtaining New Refresh Tokens
        /// 如果当前时间离refreshToken过期时间在30天以内,那么可以调用postponeToken接口换取新的refreshToken;否则会报错
        /// https://open.1688.com/api/sysAuth.htm?spm=a260s.8208020.0.0.ZL0di5&amp;ns=cn.alibaba.open
        ///
        /// </summary>
        /// <param name="clientId"></param>
        /// <param name="clientSecret"></param>
        /// <param name="accessToken"></param>
        /// <param name="refreshToken"></param>
        /// <param name="apiRoot"></param>
        /// <param name="apiVersion"></param>
        /// <param name="apiNamespace"></param>
        /// <param name="apiName"></param>
        /// <param name="protocol"></param>
        /// <param name="sessionType"></param>
        /// <param name="requestSchema"></param>
        /// <returns></returns>
        public static async Task <OAuthAccessTokenResult> RenewRefreshTokenAsync(
            string clientId,
            string clientSecret,
            string accessToken,
            string refreshToken,
            string apiRoot                = "openapi",
            string apiVersion             = "1",
            string apiNamespace           = "system.oauth2",
            string apiName                = "postponeToken",
            Com.Alibaba.Protocol protocol = Com.Alibaba.Protocol.param2,
            SessionType sessionType       = SessionType.Prod,
            UriSchema requestSchema       = UriSchema.https)
        {
            var url = new StringBuilder();

            url.Append(sessionType == SessionType.Sandbox ? Com.Alibaba.Config.RequestUriRootSandbox.UriSchemaFill(requestSchema) : Com.Alibaba.Config.RequestUriRoot.UriSchemaFill(requestSchema));
            url.Append($"/{apiRoot}/{protocol.ToString()}/{apiVersion}/{apiNamespace}/{apiName}/{clientId}");

            var data = new
            {
                client_id     = clientId,     //   Your app's client ID
                client_secret = clientSecret, //   Your app's client secret
                refresh_token = refreshToken, //    The authorization code you received
                access_token  = accessToken
            };

            return(await CommonJsonSend.SendAsync <OAuthAccessTokenResult>(null, url.ToString(), data, CommonJsonSendType.POST));
        }