Ejemplo n.º 1
0
        public async Task <string> GetAccessToken()
        {
            if (token == null || token.IsExpired())
            {
                lock (locker)
                {
                    if (token != null && !token.IsExpired())
                    {
                        return(token.access_token);
                    }

                    Dictionary <String, string> dic = new Dictionary <string, string>()
                    {
                        { "grant_type", "client_credentials" },
                        { "client_id", clientId },
                        { "client_secret", clientSecret }
                    };
                    BearerToken bt = restClient.SubmitForm <BearerToken>(_url, dic).Result;
                    if (bt != null && !String.IsNullOrEmpty(bt.access_token))
                    {
                        bt.LastGetTime = DateTime.UtcNow;
                        //Logger.Info("获取Access Token:{0}\r\n{1}", clientId, bt.access_token);
                    }
                    token = bt;
                }
            }
            return(token?.access_token);
        }