ASP.NET integration with API
Code here :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics;
namespace PivotalTrackerAPI
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e )
{
}
protected void AddStory_Click( object sender, EventArgs e )
{
// curl -H "X-TrackerToken: TOKEN" -H "Content-type: application/xml" \
//-d "<story><story_type>feature</story_type><name>Fire torpedoes</name><requested_by>James Kirk</requested_by></story>" \
//-X POST http://www.pivotaltracker.com/service...
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories?token=TOKEN";
string story = "<story><story_type>feature</story_type><name>Fire torpedoes</name><requested_by>Peter</requested_by></story>";
this.AddStoryOrTask( url, story );
}
protected void AddTask_Click( object sender, EventArgs e )
{
// curl -H "X-TrackerToken: TOKEN" -H "Content-type: application/xml" \
//-d "<task><description>clean shields</description></task>" \
//-X POST http://www.pivotaltracker.com/service...
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories/STORY_ID/tasks?token=TOKEN";
string task = "<task><description>clean the torpedo tube</description></task>";
this.AddStoryOrTask( url, task );
}
protected void GetStories_Click( object sender, EventArgs e )
{
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories?token=TOKEN";
GetStories( new Uri( url ) );
}
public static void GetStories( Uri url )
{
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
request.Proxy = WebProxy.GetDefaultProxy();
Stream objStream;
objStream = request.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader( objStream );
string sLine = "";
int i = 0;
StringBuilder sb = new StringBuilder();
while( sLine != null )
{
i++;
sLine = objReader.ReadLine();
if( sLine != null )
{
Console.WriteLine( "{0}:{1}", i, sLine );
sb.Append( sLine );
}
}
Console.ReadLine();
string xml = sb.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml( xml );
xmlDoc.Save( @"c:\stories.xml" );
StreamReader str = new StreamReader( @"c:\stories.xml" );
XmlSerializer xSerializer = new XmlSerializer( typeof( stories ) );
stories myStories = (stories)xSerializer.Deserialize( str );
foreach( storiesStory story in myStories.story )
{
Debug.Write( string.Format( "Name: {0} {1}", story.name, Environment.NewLine ) );
Debug.Write( string.Format( "Description: {0} {1}", story.description, Environment.NewLine ) );
Debug.Write( Environment.NewLine );
}
str.Close();
}
private void AddStoryOrTask( string url, string data )
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
// Set the ContentType property.
request.ContentType = "application/xml";
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
request.Proxy = WebProxy.GetDefaultProxy();
byte[] byteArray = Encoding.UTF8.GetBytes( data );
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write( byteArray, 0, byteArray.Length );
dataStream.Close();
Stream objStream;
objStream = request.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader( objStream );
string sLine = "";
int i = 0;
StringBuilder sb = new StringBuilder();
while( sLine != null )
{
i++;
sLine = objReader.ReadLine();
if( sLine != null )
{
Console.WriteLine( "{0}:{1}", i, sLine );
sb.Append( sLine );
}
}
Console.ReadLine();
string xml = sb.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml( xml );
xmlDoc.Save( @"c:\stories.xml" );
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics;
namespace PivotalTrackerAPI
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load( object sender, EventArgs e )
{
}
protected void AddStory_Click( object sender, EventArgs e )
{
// curl -H "X-TrackerToken: TOKEN" -H "Content-type: application/xml" \
//-d "<story><story_type>feature</story_type><name>Fire torpedoes</name><requested_by>James Kirk</requested_by></story>" \
//-X POST http://www.pivotaltracker.com/service...
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories?token=TOKEN";
string story = "<story><story_type>feature</story_type><name>Fire torpedoes</name><requested_by>Peter</requested_by></story>";
this.AddStoryOrTask( url, story );
}
protected void AddTask_Click( object sender, EventArgs e )
{
// curl -H "X-TrackerToken: TOKEN" -H "Content-type: application/xml" \
//-d "<task><description>clean shields</description></task>" \
//-X POST http://www.pivotaltracker.com/service...
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories/STORY_ID/tasks?token=TOKEN";
string task = "<task><description>clean the torpedo tube</description></task>";
this.AddStoryOrTask( url, task );
}
protected void GetStories_Click( object sender, EventArgs e )
{
string url = "http://www.pivotaltracker.com/services/v2/projects/PROJECT_ID/stories?token=TOKEN";
GetStories( new Uri( url ) );
}
public static void GetStories( Uri url )
{
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
request.Proxy = WebProxy.GetDefaultProxy();
Stream objStream;
objStream = request.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader( objStream );
string sLine = "";
int i = 0;
StringBuilder sb = new StringBuilder();
while( sLine != null )
{
i++;
sLine = objReader.ReadLine();
if( sLine != null )
{
Console.WriteLine( "{0}:{1}", i, sLine );
sb.Append( sLine );
}
}
Console.ReadLine();
string xml = sb.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml( xml );
xmlDoc.Save( @"c:\stories.xml" );
StreamReader str = new StreamReader( @"c:\stories.xml" );
XmlSerializer xSerializer = new XmlSerializer( typeof( stories ) );
stories myStories = (stories)xSerializer.Deserialize( str );
foreach( storiesStory story in myStories.story )
{
Debug.Write( string.Format( "Name: {0} {1}", story.name, Environment.NewLine ) );
Debug.Write( string.Format( "Description: {0} {1}", story.description, Environment.NewLine ) );
Debug.Write( Environment.NewLine );
}
str.Close();
}
private void AddStoryOrTask( string url, string data )
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create( url );
// Set the ContentType property.
request.ContentType = "application/xml";
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
request.Proxy = WebProxy.GetDefaultProxy();
byte[] byteArray = Encoding.UTF8.GetBytes( data );
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write( byteArray, 0, byteArray.Length );
dataStream.Close();
Stream objStream;
objStream = request.GetResponse().GetResponseStream();
StreamReader objReader = new StreamReader( objStream );
string sLine = "";
int i = 0;
StringBuilder sb = new StringBuilder();
while( sLine != null )
{
i++;
sLine = objReader.ReadLine();
if( sLine != null )
{
Console.WriteLine( "{0}:{1}", i, sLine );
sb.Append( sLine );
}
}
Console.ReadLine();
string xml = sb.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml( xml );
xmlDoc.Save( @"c:\stories.xml" );
}
}
}
2
people like this idea
I like this idea!
Tell me when this idea gets some attention.
The more people who like this idea, the more it gets noticed.
The more people who like this idea, the more it gets noticed.
-
Inappropriate?Thanks for the example, Michael. I've added it to our 3rd party tools directory here: http://www.pivotaltracker.com/help/th...
-
Inappropriate?Thanks for sharing the code, but the type "stories" isn't defined!
XmlSerializer xSerializer = new XmlSerializer(typeof(stories));
stories myStories = (stories)xSerializer.Deserialize(str);
foreach (storiesStory story in myStories.story)
{
Debug.Write(string.Format("Name: {0} {1}", story.name, Environment.NewLine));
Debug.Write(string.Format("Description: {0} {1}", story.description, Environment.NewLine));
Debug.Write(Environment.NewLine);
}
I’m silly
-
Inappropriate?Hi,
nice article, but how do you get the user token in the first place ?
please, can you show me an approach how to get the usertoken?
I’m happy
-
Inappropriate?You can create an API token on the My Profile page, at the bottom: https://www.pivotaltracker.com/profile
It's also possible to create or obtain a token via the API, by specifying your username and password:
https://www.pivotaltracker.com/help/a...
Loading Profile...



EMPLOYEE