我正在开发一个python应用程序,我想使用installed app flow访问我的AdWords帐户,并希望使用该页面上Distributed Apps部分中提到的最佳做法。我的AdWords API使用情况归结为:
步骤#1很简单,并且记录良好here:
import java.util.*;
public class Trip {
/** An item in the expanses list */
static class Item {
final String item;
final int cost;
public Item(String item, int cost) {
this.item = item;
this.cost = cost;
}
@Override public String toString() {
return this.item + " (" + this.cost + "$)";
}
}
/** A map with category as key and the associed list of items as value */
Map<String,List<Item>> expanses;
public Trip() {
this.expanses = new HashMap<String,List<Item>>();
String[] categories = {"food","clothes","souvenir"};
for (String cat: categories) { // init the categories with empty lists
this.expanses.put(cat, new ArrayList<Item>());
}
}
/** Register a new expanse to the trip. */
public void add(String item, int cost, String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
throw new IllegalArgumentException("Category '"+category+"' does not exist.");
list.add( new Item(item, cost) );
}
/** Get the expanses, given a category.
* @return a fresh ArrayList containing the category elements, or null if the category does not exists
*/
public List<Item> getItems(String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
return null;
return new ArrayList<Item>(list);
}
/** Get the expanses, given a category.
* @return a fresh ArrayList containing all the elements
*/
public List<Item> getItems() {
List<Item> list = new ArrayList<Item>();
for (List<Item> l: this.expanses.values()) // fill with each category items
list.addAll(l);
return list;
}
/** Get the total cost, given a category. */
public int getCost(String category) {
List<Item> list = this.expanses.get(category);
if (list == null)
return -1;
int cost = 0;
for (Item item: list)
cost += item.cost;
return cost;
}
/** Get the total cost. */
public int getCost() {
int cost = 0;
for (List<Item> l: this.expanses.values())
for (Item item: l)
cost += item.cost;
return cost;
}
// lets test this structure
public static void main(String[] args) {
Trip trip1 = new Trip();
trip1.add("apple", 10, "food");
trip1.add("cheese", 15, "food");
trip1.add("hat", 30, "clothes");
System.out.println( trip1.getItems("food") );
System.out.println( trip1.getCost("food") );
System.out.println();
System.out.println( trip1.getItems() );
System.out.println( trip1.getCost() );
}
}
步骤#2也有详细记录here:
flow = OAuth2WebServerFlow(client_id='your_client_id',
client_secret='your_client_secret',
scope='https://www.googleapis.com/auth/calendar',
redirect_uri='http://example.com/auth_return')
auth_uri = flow.step1_get_authorize_url()
credentials = flow.step2_exchange(code)
但我无法弄清楚如何仅使用访问令牌进行身份验证。 oauth2_client = oauth2.GoogleRefreshTokenClient(
client_id, client_secret, refresh_token)
adwords_client = adwords.AdWordsClient(
developer_token, oauth2_client, user_agent,
client_customer_id=client_customer_id)
课程在这里至关重要,因为这是我与AdWords API互动的方式。但我似乎无法弄清楚如何在没有googleads.adwords.AdWordsClient
对象的情况下实例化该类。并且实例化GoogleRefreshTokenClient
对象需要刷新令牌而不是访问令牌。我不想在每次需要进行AdWords API调用时实例化GoogleRefreshTokenClient
对象,因为这会迫使我每次都生成一个新的访问令牌,这会让我违反Google服务条款。