Google Adwords API 分正式API和测试API。
基中正式API是需要收取费用,Google将以API单位收费;而测试API(Sandbox)中的则不收取任何费用,但有些API返回的结果是Google定义好的,不怎么好测试!Google也是近期刚发布的V13版本,以前是V12,因为我Email账号是没缴费的账号,所以是不能用正式的API来调试Service的。充值后Google会先扣掉50块,不明所以!
如在项目中分别引用两类API:
URL:https://adwords.google.com/api/adwords/v13/AccountService?wsdl
引用命名:GoogleAdwords.AccountService
URL:https://sandbox.google.com/api/adwords/v13/AccountService?wsdl
引用命名:GoogleAdwords.Sandbox.AccountService
代码如下,分别测试时就会有如下错误:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
//正式API
using GAE.CL.GoogleAdwords.AccountService;
//测试API
//using GAE.CL.GoogleAdwords.Sandbox.AccountService;
namespace GAE.CL.ClassLibrary
{
public class Main
{
#region 常量
private static string email = "itblackhole@gmail.com";
private static string password = "******";
private static string useragent = "INSERT_COMPANY_NAME: AdWords API DotNet Sample Code";
private static string developerToken = "itblackhole@gmail.com++CNY";
private static string applicationToken = "INSERT_APPLICATION_TOKEN_HERE";
#endregion 常量
public void getClientAccounts(out string[] customerClientEmail)
{
customerClientEmail = null;
try
{
// Set up service connection.
AccountService service = new AccountService();
// Define SOAP headers.
service.emailValue = new email();
service.emailValue.Text = new String[] { email };
service.passwordValue = new password();
service.passwordValue.Text = new String[] { password };
service.useragentValue = new useragent();
service.useragentValue.Text = new String[] { useragent };
service.developerTokenValue = new developerToken();
service.developerTokenValue.Text = new String[] { developerToken };
service.applicationTokenValue = new applicationToken();
service.applicationTokenValue.Text = new String[] { applicationToken };
// Get client accounts.
string[] loginEmails = service.getClientAccounts();
// Display login emails.
if (loginEmails != null)
{
customerClientEmail = loginEmails;
foreach (string customerEmail in loginEmails)
{
HttpContext.Current.Response.Write("Login email is \"" + customerEmail + "\".<br/>");
}
}
else
{
HttpContext.Current.Response.Write("当“我的客户中心”(MCC) 帐户修改客户帐户时要使用的标头元素。<br/>");
}
}
catch (Exception e)
{
HttpContext.Current.Response.Write(e.ToString() + "<br/>");
}
}
}
}
如果使用正式的API运行上面的代码,就会报“无效的开发者令牌”,如:System.Web.Services.Protocols.SoapException: The developer token is invalid.
如果使用测试的API运行,则会正确的返回如下信息:
Login email is "client_1+itblackhole@gmail.com".
Login email is "client_2+itblackhole@gmail.com".
Login email is "client_3+itblackhole@gmail.com".
Login email is "client_4+itblackhole@gmail.com".
Login email is "client_5+itblackhole@gmail.com".