Example #1
0
        public Session Login()
        {
            OAuth1Authenticator authenticator = OAuth1Authenticator.ForProtectedResource(
                m_OAuthService.ConsumerKey, m_OAuthService.ConsumerSecret,
                m_OAuthService.AccessToken, m_OAuthService.AccessTokenSecret);

            //must be HttpAuthorizationHeader here,
            authenticator.ParameterHandling = RestSharp.Authenticators.OAuth.OAuthParameterHandling.HttpAuthorizationHeader;
            m_client.Authenticator          = authenticator;

            RestRequest request = new RestRequest("/api/v2/authentication/oxygen-login", Method.POST);

            request.RequestFormat = DataFormat.Json;
            request.AddHeader("content-type", "application/json");
            request.AddHeader("accept", "application/json");

            //The oxygen-login endpoint doesn’t read anything from the header.  So the OAuth data you are putting in the header is unnecessary.
            //One of the annoyances I have with REST is that there are multiple channels for input.
            //You have the URL, the query string, the HTTP header, the HTTP body and the HTTP verb.  Many times it’s not clear what data should go where.
            //The PLM API will favor putting data in the HTTP body whenever possible.

            OxygenCredentials cred = new OxygenCredentials();

            cred.customerId = this.customerId.ToUpper();              //must be upper case?? Yes, must be UPPERCASE!!
            cred.validation = authenticator.GetAuthorizationHeader(); // using a revised version of restsharp, authenticator.ParameterHandling should be HttpAuthorizationHeader
            request.AddBody(cred);

            IRestResponse <Session> response = m_client.Execute <Session>(request);

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                //save the cookies for latter use
                foreach (var cookie in response.Cookies)
                {
                    Cookies.Add(cookie.Name, cookie.Value);
                }

                return(response.Data);
            }
            else
            {
                return(null);
            }
        }