Need help with Proxy authentication in C#

2,163 views
Skip to first unread message

Thomas D

unread,
Jul 7, 2011, 6:56:27 AM7/7/11
to adwor...@googlegroups.com
Hello,
I'm trying to instantiate a new AdWordsUser, for that I use the App.Config file by changing the parameters:

<add key="ProxyServer" value="http://myproxy.mydomain:8080"/> 
<add key="ProxyUser" value="Thomas"/>
<add key="ProxyPassword" value="pwd"/>
<add key="ProxyDomain" value="MyDomain"/>

in my Main method:

static void Main(string[] args)
{
   AdWordsUser user = new AdWordsUser();
}

But when I run my project, I get an error 407: proxy authentication required.

My proxy parameters are correct, I have check them in another project which need proxy authentication.

Am I doing something wrong?

I'm using v201101 of the API and visual studio 2010

Thanks for your answers

Thomas

j.th...@pilot.de

unread,
Jul 14, 2011, 7:15:02 AM7/14/11
to AdWords API Forum
Hi Thomas,

did you find a solution? I face the same problem and have no idea why
this error emerges. I'd appreciate every hint that helps.
Thank you!

#Jo

Thomas D

unread,
Jul 18, 2011, 4:16:20 AM7/18/11
to adwor...@googlegroups.com
Hi Jo,

The problem comes from the AdWordsUser constructor: it try to authenticate the user before applying the proxy settings...

I use a method which get the authtoken and use another method to instantiate the AdWordsUser to solve the problem.

To get the authtoken:

public static string GetAuthToken()
        {
            WebRequest webRequest = HttpWebRequest.Create("https://www.google.com/accounts/ClientLogin");
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";

            WebProxy proxy = new WebProxy("myProxy", port);
            NetworkCredential credentials = new NetworkCredential();
            credentials.UserName = "username";
            credentials.Password = "pwd";
            credentials.Domain = "mydomain";
            proxy.Credentials = credentials;

            webRequest.Proxy = proxy;
            

            

            string postParams =
                "accountType=" + HttpUtility.UrlEncode("GOOGLE") +
                "&Email=" + HttpUtility.UrlEncode("") +
                "&Passwd=" + HttpUtility.UrlEncode("") +
                "&service=" + HttpUtility.UrlEncode("adwords") +
                "&source=" + HttpUtility.UrlEncode(string.Format("{0}-{1}-{2}", "",
                    "", "0.0"));

            byte[] postBytes = Encoding.UTF8.GetBytes(postParams);
            webRequest.ContentLength = postBytes.Length;

            using (Stream strmReq = webRequest.GetRequestStream())
            {
                strmReq.Write(postBytes, 0, postBytes.Length);
            }

            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];
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                throw new ApplicationException("Could not generate auth token.", ex);
            }
            return retVal;
        }


The AdWordsUser instantiation:

       Dictionary<String, String> dic = new Dictionary<String, String>();
        dic.Add("authToken", GetAuthToken() );
        
        AdWordsUser user = new AdWordsUser(dic);
        user.Config.Proxy.Address = new Uri("http://yourProxy:port");
        NetworkCredential cred = new NetworkCredential();
        cred.UserName="";
        cred.Password="";
        cred.Domain="";
        user.Config.Proxy.Credentials = cred;

You can use the API now!

Thomas

Kevin Winter

unread,
Jul 20, 2011, 12:39:57 PM7/20/11
to adwor...@googlegroups.com
Hi Thomas,
  Out of curiosity, what version of the .NET client library are you using?  It's possible you may be using an out of date version.

- Kevin Winter
AdWords API Team

Thomas D

unread,
Jul 21, 2011, 4:01:54 AM7/21/11
to adwor...@googlegroups.com
I use the v201101 of the client library

AdWords API Advisor

unread,
Jul 21, 2011, 4:26:57 AM7/21/11
to adwor...@googlegroups.com
Hi Thomas,

Which version of the library (not the API) are you using? The latest version is 12.4.0. I'm trying to replicate the issue you are mentioning, but so far I'm out of luck.

Cheers,
Anash P. Oommen,
AdWords API Advisor.

Thomas D

unread,
Jul 21, 2011, 4:38:59 AM7/21/11
to adwor...@googlegroups.com
Hi Anash,
It's the version 12.3.0

AdWords API Advisor

unread,
Jul 21, 2011, 1:30:53 PM7/21/11
to adwor...@googlegroups.com
Hi Thomas,

I still can't replicate this issue. I'm using v12.4.0, but the proxy code hasn't changed from v12.3.0. Here's how I am testing, that may give a hint about what's going wrong.

- Setup a proxy using squid: http://news.softpedia.com/news/Seting-Up-a-HTTP-Proxy-Server-with-Authentication-and-Filtering-52467.shtml
- Downloaded http://google-api-adwords-dotnet.googlecode.com/files/awapi_dotnet_lib_src_12.4.0.zip.
- Extracted the project and opened AdWords.v2008.sln in VS 2008.
- Set the proxy url, username, password in examples\csharp\App.config.
- Set Developer token, Email, password, ClientEmail in examples\csharp\App.config
- Run AdWords.Examples.CSharp with command line argument as v201101.GetAllCampaigns.

My call fails for wrong username/password (407 Proxy Authentication required) and succeeds for the right username/password in App.config.
Reply all
Reply to author
Forward
0 new messages