コード例 #1
0
        public string GetToken()
        {
            if (!_authenticationToken.HasToken() || _authenticationToken.HasExpired())
            {
                string         url     = _marketoInstanceUrl + "identity/oauth/token?grant_type=client_credentials&client_id=" + _marketoClientId + "&client_secret=" + _marketoClientSecret;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.ContentType = "application/json";
                HttpWebResponse             response  = (HttpWebResponse)request.GetResponse();
                Stream                      resStream = response.GetResponseStream();
                StreamReader                reader    = new StreamReader(resStream);
                string                      json      = reader.ReadToEnd();
                AuthenticationTokenResponse authenticationTokenResponse = JsonConvert.DeserializeObject <AuthenticationTokenResponse>(json);

                _authenticationToken = new AuthenticationToken()
                {
                    AccessToken = authenticationTokenResponse.Access_token,
                    Expires     = DateTime.Now.AddSeconds(authenticationTokenResponse.Expires_in - 500)
                };
            }

            return(_authenticationToken.AccessToken);
        }