Try running the example get_campaign.py in the google ads api.
The code was entered as follows
import argparse
import sys
from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException
def main(client, customer_id):
ga_service = client.get_service("GoogleAdsService", version="v6")
query = """
SELECT campaign.id, campaign.name
FROM campaign
ORDER BY campaign.id"""
# Issues a search request using streaming.
response = ga_service.search_stream(customer_id, query=query)
try:
for batch in response:
for row in batch.results:
print(
f"Campaign with ID {row.campaign.id} and name "
f'"{row.campaign.name}" was found.'
)
except GoogleAdsException as ex:
print(
f'Request with ID "{ex.request_id}" failed with status '
f'"{ex.error.code().name}" and includes the following errors:'
)
for error in ex.failure.errors:
print(f'\tError with message "{error.message}".')
if error.location:
for field_path_element in error.location.field_path_elements:
print(f"\t\tOn field: {field_path_element.field_name}")
sys.exit(1)
if __name__ == "__main__":
# GoogleAdsClient will read the google-ads.yaml configuration file in the
# home directory if none is specified.
google_ads_client = GoogleAdsClient.load_from_storage('C:/Users/GoogleAPI/googleads.yaml')
parser = argparse.ArgumentParser(
description="Lists all campaigns for specified customer."
)
# The following argument(s) should be provided to run the example.
parser.add_argument(
"-c",
"--customer_id",
type=str,
required=True,
help="The Google Ads customer ID.",
)
args = parser.parse_args()
print(args.customer_id)
main(google_ads_client, args.customer_id)
But I get this error.
errors {
error_code {
authorization_error: USER_PERMISSION_DENIED
}
message: "User doesn\'t have permission to access customer. Note: If you\'re accessing a client customer, the manager\'s customer id must be set in the \'login-customer-id\' header. See https://developers.google.com/google-ads/api/d
ocs/concepts/call-structure#login-customer-id"
}
customer id works fine for google adwords api.
If anyone knows the answer, I would appreciate it if you let me know.
Thank you.
The only thing you need is to pass the CustomerID without dash (-). More specifically, you just need passing the CustomerID with XXXXXXXXXX format instead of XXX-XXX-XXXX. The documentation isn't that informative!
Related
I am trying to send e-mail campaign mails with the SMTP API of Sendinblue.com, a cloud-based marketing communication software suite. I am running my configuration of the template script provided by Sendinblue as seen on the screenshot:
My initial script was as follows:
# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-.................................' # full api key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "------#gmail.com"}, # personal email with which I was registered # sendinblue
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
api_response = api_instance.create_email_campaign(email_campaigns)
pprint(api_response)
except ApiException as e:
print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
I referred to Sendinblue Documentation in the cases where I had doubt. Most of the process seems self-explanatory, just the line with configuration.api_key['api-key'] = 'YOUR API KEY' was ambiguous as it is not quite clearly stated how exactly the api-key should be passed and the attribute of api_key I assumed api_key was holding the name of the API as defined in the SMTP & API Advanced tab. Even if I assume it should be holding some other values, it is not clear what they should be, what I can tell for sure is that sib_api_v3_sdk.configuration.api_key['api-key'] also resulted in AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key'.
After initially getting the error AttributeError: module 'sib_api_v3_sdk.configuration' has no attribute 'api_key' I researched a dozen of articles on StackOverflow, one of them on the Kubernetes topic where the script seemed to throw a similar error like the one I was getting, so I followed the advices described in Python kubernetes module has no attribute 'api_key'. Therefore, I tried reassigning the class attributes as follows:
configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
The fact that now I wasn't getting the error of a missing key but facing an 'unauthorized' message cheered me up for a short time, until I spent several hours trying to get past this. So the script that's now throwing the HTTP response body: {"message":"Key not found","code":"unauthorized"} is now this:
# ------------------
# Create a campaign\
# ------------------
# Include the Sendinblue library\
from __future__ import print_function
import time
from datetime import datetime
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
myapikey = 'xkeysib-c..............' # key
# Instantiate the client\
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
# Define the campaign settings\
email_campaigns = sib_api_v3_sdk.CreateEmailCampaign(
name= "Campaign sent via the API",
subject= "My subject",
sender= { "name": "From name", "email": "mail....#gmail.com"}, # personal gmail with which I was initially registered # sendinblue.com
type= "classic",
# Content that will be sent\
html_content= "Congratulations! You successfully sent this example campaign via the Sendinblue API.",
# Select the recipients\
recipients= {"listIds": [2, 7]},
# Schedule the sending in one hour\
scheduled_at = datetime.now()
)
# Make the call to the client\
try:
api_response = api_instance.create_email_campaign(email_campaigns)
pprint(api_response)
except ApiException as e:
print("Exception when calling EmailCampaignsApi->create_email_campaign: %s\n" % e)
My 1st question is: was my 1st script wrong and in what way?
Secondly: Is configuration.api_key['api-sb'] = myapikey the correct syntax, assuming that 'api-sb' is the name of my API key as shown on the screenshot?
And third: In the myapikey variable, when assigning the API key itself, should there be anything else as a prefix to the key?
Try this sample transactional email script. Worked for me in python.
https://developers.sendinblue.com/reference/sendtransacemail
Instead of this:
sib_api_v3_sdk.configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi()
do this:
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-sb'] = myapikey
api_instance = sib_api_v3_sdk.EmailCampaignsApi(sib_api_v3_sdk.ApiClient(configuration))
as shown in https://developers.sendinblue.com/reference/createemailcampaign-1.
I'm trying to get search queries for my site.
import argparse
import sys
from googleapiclient import sample_tools
def execute_request(service, property_uri, request):
"""Executes a searchAnalytics.query request.
Args:
service: The webmasters service to use when executing the query.
property_uri: The site or app URI to request data for.
request: The request to be executed.
Returns:
An array of response rows.
"""
return service.searchanalytics().query(
siteUrl=property_uri, body=request).execute()
# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument('property_uri', type=str,
help=('Site or app URI to query data for (including '
'trailing slash).'))
argparser.add_argument('start_date', type=str,
help=('Start date of the requested date range in '
'YYYY-MM-DD format.'))
argparser.add_argument('end_date', type=str,
help=('End date of the requested date range in '
'YYYY-MM-DD format.'))
service, flags = sample_tools.init(
sys.argv, 'webmasters', 'v3', __doc__, 'client_secrets.json', parents=[argparser],
scope='https://www.googleapis.com/auth/webmasters.readonly')
# First run a query to learn which dates we have data for. You should always
# check which days in a date range have data before running your main query.
# This query shows data for the entire range, grouped and sorted by day,
# descending; any days without data will be missing from the results.
request = {
'startDate': flags.start_date,
'endDate': flags.end_date,
'dimensions': ['date']
}
response = execute_request(service, flags.property_uri, request)
print(response)
When I run the program:
python googleapisearch.py property_uri=http://enquetemaken.be/ start_date=2018-06-12 end_date=2018-06-13
I get the following error:
googleapiclient.errors.HttpError: https://www.googleapis.com/webmasters/v3/sites/property_uri%3Dhttp%3A%2F%2Fenquetemaken.be%2F/searchAnalytics/query?alt=json
returned "'property_uri=http://enquetemaken.be/' is not a valid Search
Console site URL.">
I can not understand what's wrong.
In the dashboard, my url is exactly the same as I enter:
What am I doing wrong?
Correctly run the program as follows:
python googleapisearch.py 'http://enquetemaken.be/' '2018-06-12' '2018-06-13'
I want to send transactional and marketing emails using 'SendInBlue'. I also want to use Python language to do the same. I have visited the API doc of SendInBlue and followed the same procedure, still unsuccessful in sending the emails.
from mailin import Mailin
m = Mailin("https://api.sendinblue.com/v2.0","ScrWGqd296ya0CWq")
data = { "to" : {"aman#gmail.com":"to whom!"},
"from" : ["amandeep#gmail.com", "from email!"],
"subject" : "Subject...",
"html" : "This is the <h1>HTML</h1>",
"attachment" : ["https://example.com/path-to-file/filename1.pdf", "https://example.com/path-to-file/filename2.jpg"]
}
result = m.send_email(data)
print(result)
I have also downloaded mailin-api-python from github and ran this script. I don't have any idea where to setup to my smtp details.
**I have changed the API key just for security purpose.
I would strongly recommend you to use the latest version of SendinBlue's Python wrapper where they have provided an example
from __future__ import print_function
import time
import sib_api_v3_sdk
from sib_api_v3_sdk.rest import ApiException
from pprint import pprint
# Configure API key authorization: api-key
configuration = sib_api_v3_sdk.Configuration()
configuration.api_key['api-key'] = 'API-KEY'
# create an instance of the API class
api_instance = sib_api_v3_sdk.SMTPApi(sib_api_v3_sdk.ApiClient(configuration))
senderSmtp = sib_api_v3_sdk.SendSmtpEmailSender(name="test",email="youremail#gmail.com")
sendTo = sib_api_v3_sdk.SendSmtpEmailTo(email="recipientEmail#gmail.com",name="Recipient Name")
arrTo = [sendTo] #Adding `to` in a list
send_smtp_email = sib_api_v3_sdk.SendSmtpEmail(sender=senderSmtp,to=arrTo,html_content="This is a test",subject="This is a test subject") # SendSmtpEmail | Values to send a transactional email
try:
# Send a transactional email
api_response = api_instance.send_transac_email(send_smtp_email)
pprint(api_response)
except ApiException as e:
print("Exception when calling SMTPApi->send_transac_email: %s\n" % e)
I got a sample script working:
I successfully received the email and the messageId as the response.
Please let me know if it helps!
The problem:
I wrote a python script using boto to retrieve all unused security groups by their name. I also have tried by id because the documentation reads
delete_security_group(name=None, group_id=None, dry_run=False)
Delete a security group from your account.
Parameters:
name (string) – The name of the security group to delete.
group_id (string) – The ID of the security group to delete within a VPC.
dry_run (bool) – Set to True if the operation should not actually run.
Return type:
bool
Returns:
True if successful.
so technically deletion by name should make it vpc ambivalent, either way i have tried both. However, boto returns an error about deleting the security group from the wrong vpc. I'm a little confused here.
Here is the error
group for delete elb
EC2ResponseError: 400 Bad Request
<?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>InvalidGroup.NotFound</Code><Message>The security group 'elb' does not exist in default VPC 'vpc-4fb'</Message></Error></Errors><RequestID>5c72aeb6-e841-4f3b-b976-4aa02b806a77</RequestID></Response>
this was ran against scratch. You can see the error message is for the default vpc and not the vpc that my groups live in. I try to solve this by filtering by vpc_id but still same error.
#!/usr/bin/env python
import argparse
from boto import ec2
import os
import pprint
import sys
def main(profile=None, region='us-west-2', del_flag=None):
if profile =='production':
vpc = "vpc-efe"
if profile =='dev':
vpc = "vpc-139"
if profile =='test':
vpc = "vpc-ecd"
if profile =='scratch':
vpc = "vpc-475"
pp = pprint.PrettyPrinter(indent=4)
conn = ec2.connect_to_region(region, profile_name=profile)
allgroups = []
# Get ALL security groups names
vpc_filter = {'vpc_id':vpc}
groups = conn.get_all_security_groups(filters=vpc_filter)
for groupobj in groups:
allgroups.append(groupobj.name)
# Get [running|stopped] instances security groups
groups_in_use = []
for state in ['running','stopped']:
reservations = conn.get_all_instances(filters={'instance-state-name': state})
for r in reservations:
for inst in r.instances:
if inst.groups[0].name not in groups_in_use:
groups_in_use.append(inst.groups[0].name)
delete_candidates = []
for group in allgroups:
if group not in groups_in_use:
delete_candidates.append(group)
if del_flag == 'yes':
print "We will now delete security groups identified to not be in use."
for group in delete_candidates:
print 'group for delete', group
try:
conn.delete_security_group(group)
except Exception as e:
print e
print "We have deleted %d groups." % (len(delete_candidates))
print "The list of security groups that are in use."
pp.pprint(sorted(groups_in_use))
print "Total of %d groups targeted for being in use." % (len(groups_in_use))
else:
print "The list of security groups to be removed is below."
print "Run this again with `--delete` to remove them"
pp.pprint(sorted(delete_candidates))
print "Total of %d groups targeted for removal." % (len(delete_candidates))
print "The list of security groups that are in use."
pp.pprint(sorted(groups_in_use))
print "Total of %d groups targeted for being in use." % (len(groups_in_use))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--profile', help='profile name in your ~/.boto config', required=True)
parser.add_argument('--delete', help='delete yes or no', required=True)
parser.add_argument('--region', help='', required=True)
args = parser.parse_args()
main(profile=args.profile, region=args.region, del_flag=args.delete)
You must pass the security group as a keyword argument. I've created a dummy group named 'delete_me_test_from_boto'.
When I run:
conn.delete_security_group('delete_me_test_from_boto')
I get the following error:
EC2ResponseError: EC2ResponseError: 400 Bad Request
InvalidGroup.NotFoundThe security group 'delete_me_test_from_boto' does not exist in default VPC 'vpc-9efe64fb'c89e06e8-2d39-4365-b326-84f5a4896980
However, this works:
conn.delete_security_group(group_id='sg-e1085f85')
With Boto3 you can use the SecurityGroup resource to do this:
import boto3
ec2 = boto3.resource('ec2')
def delete_security_group(group_id):
try:
ec2.SecurityGroup(group_id).delete()
logger.info("Deleted security group %s.", group_id)
except ClientError:
logger.exception("Couldn't delete security group %s.", group_id)
raise
This is part of a larger example on GitHub awsdocs/aws-doc-sdk-examples
I wrote a web service in python to accept the DatasetName and the TableName as inputs in url which will be passed to big query and the fields will be returned as output.
I have used python client for bigquery and accessing the schema information like this, but am not able to get the result as i expect.
it returns "Invalid dataset ID "publicdata:samples". Dataset IDs must be alphanumeric (plus underscores, dashes, and colons) and must be at most 1024 characters long.">"
import web
from bigquery import get_client
urls = (
'/GetFields(.*)', 'get_Fields'
)
app = web.application(urls, globals())
class get_Fields:
def GET(self,r):
datasetname = web.input().datasetName
tablename = web.input().tableName
# BigQuery project id as listed in the Google Developers Console.
project_id = 'din-1085'
# Service account email address as listed in the Google Developers Console.
service_account = '101363222700-epqo6lmkl67j6u1qafha9dke0pmcck3#developer.gserviceaccount.com'
# PKCS12 or PEM key provided by Google.
key = 'Digin-d2421e7da9.p12'
client = get_client(project_id, service_account=service_account,
private_key_file=key, readonly=True)
# Submit an async query.
job_id, _results = client.get_table_schema(datasetname,tablename)
# Retrieve the results.
return results
if __name__ == "__main__":
app.run()
This is the data that i pass:
http://localhost:8080/GetFields?datasetName=publicdata:samples&tableName=shakespeare
Dataset name :publicdata:samples
TableName : shakespeare
Expected Output:
word
word_count
corpus
corpus_date
Finally made it work by changing this line
From:
# Submit an async query.
job_id, _results = client.get_table_schema(datasetname,tablename)
To:
# Submit an async query.
results = client.get_table_schema(datasetname,tablename)