3

Ok so I've read multiple articles on my topic but none seem to help. I'm trying to accomplish the same thing this guy wanted in this article here. I used the example stated in the answer (https://{yourserver}/defaultcollection/_apis/git/repositories?api-version=1.0 ) in an angular http get request but I'm getting a 401 error saying the credentials i supplied were not valid.

I then downloaded curl and supplied the following command "curl -u username:password http://{myserver}/defaultcollection/_apis/git/repositories?api-version=1.0" and the result came back the same, html saying i do not have valid credentials. Can someone point me in the right direction so I can pull the projects root branch and childrens branches and data along with it?

Essentially I am searching for a way to pull data from the server and display it on my web app. I'll need branches names and history for each branch and child branch.

Update

So I'm now able cURL into this service using the --ntlm switch that Richard recommended. Now to actually get my app connected to the service I found that I could pass windows credentials using:

withCredentials:true

It no longer gives me a 401 error. Instead I am not getting a new error stating:

XMLHttpRequest cannot load http://{servername}/{defaultcollection}/_apis/projects?api-version=1.0. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

What are my next steps to get this working?

Solution

I took what Richard suggested and created my own asp.net web api to communicate with tfs. I was able to modify the web.config file to allow CORS. I added the following lines to the system.webServer section of the file.

<httpProtocol>
   <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
   </customHeaders>
</httpProtocol>

Doing this has resolved the error message I was receiving and creating a simple api to get specific information was not too hard to do. Thanks for all the help.

Community
  • 1
  • 1
itsNino91
  • 129
  • 15

1 Answers1

5

In your curl command add --ntlm as an option.

The command should be as follows:

curl -u username:password --ntlm http://{myserver}/defaultcollection/_apis/git/repositories?api-version=1.0

Be aware that the API doesn't currently support CORS. This means you can make API calls from your own server side code without a problem, but you can't make the calls directly from a browser. There's a UserVoice suggestion to enable this that you can vote for.

Richard Banks
  • 12,456
  • 3
  • 46
  • 62
  • thanks for that @RichardBanks. It helped with allowing me to cURL into what i was looking for. However, the question still remains how do i do this in angularjs? – itsNino91 Mar 27 '15 at 13:14
  • I've tried passing credentials with the http.get function. This give me a 200 for accessing the url but I now get an error saying - " A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost' is therefore not allowed access." – itsNino91 Mar 27 '15 at 13:48
  • Ah. Yeah. Sorry, I missed that. I'll update the answer. – Richard Banks Mar 30 '15 at 04:09
  • Is there any known work around to get this working? Or am i just dead in the water? – itsNino91 Mar 31 '15 at 13:54
  • I wrote server side code to do the API calls & other processing I wanted and then exposed that to a browser through my own API. You could do the same, or you could create mirror the API calls with your own using Node or Web API for example, and add CORS support to your version of the API methods. Not ideal. – Richard Banks Mar 31 '15 at 19:43
  • I do not have access to modify the server code unfortunately. This is an internal web app only so I'm not too worried about security. Could you explain what you mean by "You could do the same, or you could create mirror the API calls with your own using Node or Web API for example, and add CORS support to your version of the API methods". I appreciate your help. – itsNino91 Apr 01 '15 at 12:06
  • As of February 2016 this feature is included (https://visualstudio.uservoice.com/forums/330519-team-services/suggestions/6589478-support-for-cors-in-the-visual-studio-online-rest) – Dmitri Tsoy Sep 22 '16 at 12:35