I have an app that is currently using the v2 api and now that is never going to leave tech preview, I am moving things over to v1 api, but something weird is happening.
I have tried using fiddler and both RestSharp and HttpClient libraries to figure out what is going on.
I am able to successfully login to plm360 using the v1 api and get back a loginresponse object with the session id. Next I try to get the workspaces (api/rest/v1/workspaces). Using the HttpRequestMessage I can print out the contents of my request:
Method: GET, RequestUri: '/api/rest/v1/workspaces', Version: 1.1, Content: <null>, Headers: { accept: application/json cookie: customer=ZZZZZZZZZZ;JSESSIONID=E01D15CAD6085AA0EC783XXXXXXXX1F0C.worker1 }
When I execute the request, I get internal server error (500). However, if I take the headers from above and execute the request in Fiddler, I get back the list of workspaces.
I get the same behavior in RestSharp.
Does anyone have an idea of what I am doing wrong??
Thanks
Mike
Solved! Go to Solution.
Solved by jared.sund. Go to Solution.
Hello Mike,
Below is a very basic C# class example that provides functions to log into PLM 360 and retireve a list of workspaces. This is working with XML, but you could easily change it over to JSON.
Hope this helps,
-Jared
Basic usage of the class
public partial class Form1 : Form { basicPLMV1 myPLM; public Form1() { InitializeComponent(); myPLM = new basicPLMV1("sometenantNameHere"); label1.Text = "false"; } private void button1_Click(object sender, EventArgs e) { label1.Text = myPLM.login("myusername", "mypassord").ToString(); } private void button2_Click(object sender, EventArgs e) { XmlDocument results = myPLM.getWorksapces(); textBox1.Text = results.OuterXml.ToString(); } }
Sample Class
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml; namespace sampleApp { class basicPLMV1 { private string _tenantURI; private string _authCookie; private bool _loggedIN; public basicPLMV1(string tenantName) { _tenantURI = String.Format("https://{0}.autodeskplm360.net/", tenantName); _loggedIN = false; _authCookie = ""; } public bool login(string userName, string password) { string tenantURIinput = _tenantURI + "rest/auth/1/login.xml"; string payload = String.Format ("<authCommand><userID>{0}</userID><password>{1}</password></authCommand>", userName, password ); HttpWebResponse response = post(tenantURIinput, payload); XmlDocument xmlDoc = new XmlDocument(); using (XmlReader reader = XmlReader.Create(response.GetResponseStream(), new XmlReaderSettings() { CloseInput = true })) { xmlDoc.Load(reader); } if (xmlDoc.DocumentElement.SelectSingleNode("/userAuthInfo/authStatus/id").InnerText == "200") { _authCookie = response.Headers["Set-Cookie"]; _loggedIN = true; return true; } else { _authCookie = ""; _loggedIN = false; return false; } }//end function login public XmlDocument getWorksapces() { if (!_loggedIN) { return null; } XmlDocument xmlDoc = new XmlDocument(); HttpWebRequest request = getHTTPRequest(_tenantURI + "api/rest/v1/workspaces.xml", "GET"); request.Headers.Add(HttpRequestHeader.Cookie, _authCookie); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (XmlReader reader = XmlReader.Create(response.GetResponseStream(), new XmlReaderSettings() { CloseInput = true })) { xmlDoc.Load(reader); } return xmlDoc; } private HttpWebResponse post(string uri, string payload) { XmlDocument doc = new XmlDocument(); HttpWebRequest request = getHTTPRequest(uri, "POST"); request.Headers.Add(HttpRequestHeader.Cookie, _authCookie); UTF8Encoding encoding = new UTF8Encoding(); request.ContentLength = encoding.GetByteCount(payload); request.ContentType = "application/xml"; using (Stream reqStream = request.GetRequestStream()) { reqStream.Write(encoding.GetBytes(payload), 0, encoding.GetByteCount(payload)); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); return response; } private HttpWebRequest getHTTPRequest(string uri, string method) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Method = method; request.Accept = "application/xml"; return request; } } }
Thank you Jared, that was really helpful.
I am still unsure of one thing though ... when I was doing it manually in fiddler, the auth cookie looks much different but still works. Is that fiddler just filling in some missing items for me?
Thanks again,
Mike
The auth cookie can look a little different each time you login. It may or may not include load balancing information, depeding on how the system routes your session. This could be the difference you're seeing.
-Jared
Hello mike im trying to get a succesfull login with v1 api but im not able to authenticate I get a LOGIN_FAILED i believe i have domething missing in my authentication data or something
Thanks
Hi Yader, LOGIN_FAILED sounds to me like invalid credentials or something else wrong with your POST to the login endpoint. I have always used json for my post data and the field names are all case sensitive so make sure your POST data matches what is in the api documentation (login api page). I assume that you have covered off all of the other basics:
Hope that helps a bit.
Hello mike ,i appreciate your help.I did as the documentation says before I asked ,and after i re-checked but still not able to log-in, I believe that the 2 parameters I´ve sent by JSON are not enought ,I also checked the correctness of the password(I came to conclusion that is probably user/password related but still not logged),so im currently clueless,thanks for the help and will keep trying
Hi Yader, according to the docs, you only post the user id and password when you do the api call (and is all that I supply on login). The only other thing I could suggest is that you make sure you clear out any default headers that are being included when you make your REST call. Sometimes the api can get confused if your headers are incorrect. I had this happen to me with downloading images using restsharp and I had it happen when using the rest libraries that are in the universal windows platform.
Good luck!
Hello Mike ,this is the code of my rest function.I supplied the headers(i believe) and the paramenters
,maybe from here you get a clear idea.It should be straight forward but the strange thing is the response,
that it behave like the authentication is incorrect,but i tested on the web my credentials
Thanks
function login(url, postMethod) {
item.EXTERNAL_DATA = "";
var xmlhttp = getHTTPObject();
xmlhttp.open(postMethod, url, true);
xmlhttp.setRequestHeader("Content-type", "application/json");
xmlhttp.setRequestHeader("Accept", "application/json");
var parameters = {
"userID": "t_a360user",
"password": "p"55w0rd"
}
try {
xmlhttp.send(JSON.stringify(parameters));
item.EXTERNAL_DATA = xmlhttp.responseText;
}
catch (err) {
item.EXTERNAL_DATA = err.message;
}
}