I am trying to get auth token and add campaign using adwords sandbox
api and I am getting internal server error.
private static string GetAuthToken()
{
WebRequest webRequest = HttpWebRequest.Create("https://
www.google.com/accounts/ClientLogin");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-
urlencoded";
string postParams = "accountType=" +
HttpUtility.UrlEncode("GOOGLE") +
"&Email=" +
HttpUtility.UrlEncode("[email protected]") +
"&Passwd=" + HttpUtility.UrlEncode("xxxxxxxx") +
"&service=" + HttpUtility.UrlEncode("adwords") +
"&source=" + HttpUtility.UrlEncode(string.Format("{0}-
{1}-{2}", "Bizlabxx", "Adwords API Testing", "v201101")) +
"&developertoken=" +
HttpUtility.UrlEncode("[email protected]++USD") +//+
// "&userAgent=" + HttpUtility.UrlEncode("Adwords API
Testing") +
"&clientemail=" +
HttpUtility.UrlEncode("[email protected]");
byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
webRequest.ContentLength = postBytes.Length;
using (Stream strmReq = webRequest.GetRequestStream())
{
// strmReq.Write(postBytes, 0, postBytes.Length);
StreamWriter myStreamWriter;
myStreamWriter = new StreamWriter(strmReq);
myStreamWriter.Write(postParams);
myStreamWriter.Flush();
myStreamWriter.Close();
strmReq.Close();
}
string retVal = "";
try
{
WebResponse response = webRequest.GetResponse();
using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
{
string sResponse = reader.ReadToEnd(); string[]
splits = sResponse.Split('\n');
foreach (string split in splits)
{
string[] subsplits = split.Split('=');
if (subsplits.Length >= 2 && subsplits[0] ==
"Auth")
{
retVal = subsplits[1];
}
}
reader.Close();
}
}
catch (WebException ex)
{
throw new System.ApplicationException("Could not
generate auth token.", ex);
}
return retVal;
}
private static string CreateCampaign()
{
string authToken = GetAuthToken();
string responseXml = "";//<applicationToken>{0}</
applicationToken> <userAgent>{3}</userAgent>, "Adwords API
Testing"
string requestXml = string.Format(@"
<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
<soap:Header>
<RequestHeader xmlns='https://adwords-sandbox.google.com/api/adwords/
cm/v201101'>
<authToken>{0}</authToken>
<clientEmail>{1}</clientEmail>
<developerToken>{2}</developerToken>
</RequestHeader>
</soap:Header>
<soap:Body>
<mutate xmlns='https://adwords-sandbox.google.com/api/adwords/cm/
v201101'>
<operations>
<operator>ADD</operator>
<operand>
<name>Interplanetary Cruise#111</name>
<status>PAUSED</status>
<budget>
<period>DAILY</period>
<amount>
<microAmount>50000000</microAmount>
</amount>
<deliveryMethod>STANDARD</deliveryMethod>
</budget>
<biddingStrategy xsi:type='ManualCPC' />
</operand>
</operations>
</mutate>
</soap:Body>
</soap:Envelope>",
authToken,"[email protected]",
"[email protected]++USD").Trim();
WebRequest webRequest = HttpWebRequest.Create(
"https://adwords-sandbox.google.com/api/adwords/cm/
v201101/CampaignService");
webRequest.Method = "POST";
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Headers.Add("SOAPAction", "");
byte[] postBytes = Encoding.UTF8.GetBytes(requestXml);
webRequest.ContentLength = postBytes.Length;
using (Stream strmReq = webRequest.GetRequestStream())
{
// strmReq.Write(postBytes, 0, postBytes.Length);
StreamWriter myStreamWriter;
myStreamWriter = new StreamWriter(strmReq);
myStreamWriter.Write(requestXml);
myStreamWriter.Flush();
myStreamWriter.Close();
strmReq.Close();
}
try
{
WebResponse response = webRequest.GetResponse();
using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
{
responseXml = reader.ReadToEnd();
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(responseXml);
XmlNamespaceManager xmlns = new
XmlNamespaceManager(xDoc.NameTable);
xmlns.AddNamespace("soap", "http://
schemas.xmlsoap.org/soap/envelope/");
xmlns.AddNamespace("v2011", "https://adwords-
sandbox.google.com/api/adwords/cm/v201101");
XmlElement faultNode =
(XmlElement)xDoc.SelectSingleNode("soap:Envelope/soap:Body/
soap:Fault", xmlns);
if (faultNode != null)
{
// Something went wrong with API call. Parse
faultNode for more details.
}
else
{
XmlElement mutateResponseNode =
(XmlElement)xDoc.SelectSingleNode(
"soap:Envelope/soap:Body/
v2011:mutateResponse", xmlns);
// Parse mutateResponseNode contents to get
the campaign id.
}
reader.Close();
}
}
catch (WebException ex)
{
throw new System.ApplicationException("Could not make
Api call.", ex);
}
return authToken + " " + responseXml;
}
Thanks,
Soumya.
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog and discussion group:
http://adwordsapi.blogspot.com
http://groups.google.com/group/adwords-api
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en