Facebook Business API request campaigns via python - python

We are busy upgrading from the Marketing API to the Business API (not graph API and not marketing API) for Facebook. The documentation states that one should be able to create a test.py with the following code:
import sys
sys.path.append('/opt/homebrew/lib/python2.7/site-packages')
sys.path.append('/opt/homebrew/lib/python2.7/site-packages/facebook_business-3.0.0-py2.7.egg-info')
from facebook_business.api import FacebookAdsApi
from facebook_business.adobjects.adaccount import AdAccount
my_app_id = 'your-app-id'
my_app_secret = 'your-appsecret'
my_access_token = 'your-access-token'
FacebookAdsApi.init(my_app_id, my_app_secret, my_access_token)
my_account = AdAccount('your-adaccount-id')
campaigns = my_account.get_campaigns()
print(campaigns)
This requires an app secret proof which I am obtaining with the following hash:
facebook_app_id = 'xxxxx'
facebook_app_secret = 'xxxxx'
facebook_app_token = 'xxxxx|xxxxx'.format(facebook_app_id,facebook_app_secret)
import hmac,hashlib
app_secret_proof = hmac.new(facebook_app_secret.encode('utf-8'),
msg=facebook_app_token.encode('utf-8'),
digestmod=hashlib.sha256).hexdigest()
print(app_secret_proof)
The is the error I get:
Traceback (most recent call last):
File "test.py", line 12, in <module>
FacebookAdsApi.init(my_app_id,my_app_secret,my_access_token,my_appsecret_proof)
File "/Users/facebook_business/api.py", line 202, in init
cls.set_default_account_id(account_id)
File "/Users/facebook_business/api.py", line 225, in set_default_account_id
"Account ID provided in FacebookAdsApi.set_default_account_id "
ValueError: Account ID provided in FacebookAdsApi.set_default_account_id expects a string that begins with 'act_'
None of the ID's start with act_, I am currently using the App ID that is at the top left of the app dashboard which one should I be using?
Marketing API

The Ad account number in business manager is obtained on the front log in screen.
From Business Manager

Related

Pythonanywhere: Twitter API authenticates from bash, but fails on scheduled task

I'm using the python-twitter (not tweepy) module in my application.
I have a script set up (maketweet.py) that, when I run it from bash / console, works successfully (i.e. it makes a tweet).
The problem I'm having is that when I run the same script as a scheduled task, I get an error:
Traceback (most recent call last):
File "/home/dir1/dir2/proj/maketweet.py", line 21, in <module>
api = create_api()
File "/home/dir1/dir2/proj/config.py", line 31, in create_api
if api.VerifyCredentials():
File "/home/dir1/.local/lib/python3.8/site-packages/twitter/api.py", line 4699, in VerifyCredentials
resp = self._RequestUrl(url, 'GET', data)
File "/home/dir1/.local/lib/python3.8/site-packages/twitter/api.py", line 4959, in _RequestUrl
raise TwitterError("The twitter.Api instance must be authenticated.")
twitter.error.TwitterError: The twitter.Api instance must be authenticated.
The other problem I'm having is that I can't conceive why it would make a difference that I'm using a scheduled task, rather than running the file directly from bash. Here are the contents of config.py:
#!/usr/bin/python3.8
import twitter
import os
import logging
from dotenv import load_dotenv
logger = logging.getLogger()
# project folder is one level up from file location
project_folder = pathlib.Path(__file__).parent.absolute()
load_dotenv(os.path.join(project_folder, '.env'))
# Authenticate to Twitter and create API object
TWITTER_CONSUMER_API_KEY = os.getenv("TWITTER_CONSUMER_API_KEY")
TWITTER_CONSUMER_API_SECRET = os.getenv("TWITTER_CONSUMER_API_SECRET")
TWITTER_ACCESS_TOKEN = os.getenv("TWITTER_ACCESS_TOKEN")
TWITTER_ACCESS_SECRET = os.getenv("TWITTER_ACCESS_SECRET")
def create_api():
# Create API object
api = twitter.Api(
access_token_key = TWITTER_ACCESS_TOKEN,
access_token_secret = TWITTER_ACCESS_SECRET,
consumer_key = TWITTER_CONSUMER_API_KEY,
consumer_secret = TWITTER_CONSUMER_API_SECRET,
sleep_on_rate_limit=True)
# test API object
if api.VerifyCredentials():
pass
else:
logger.error("Error creating API", exc_info=True)
raise Exception("Twitter user authentication error")
logger.info("API created")
return api
Obviously there's an error in creating the API. I imagine this has something to do with the environment variables and how they are accessed through scheduled tasks vs. bash. Just really not sure how to figure this one out...

micropython - ESP32 - MQTT Exception 4 - Google Cloud IoT

I am looking to send sensor data through an ESP32 board using micropython to Google IoT Cloud.
I found an implementation example here: https://nihal-pasham.medium.com/you-could-try-freezing-the-pyasn-1-9afd6533cd7a
But it is throwing the following error message:
Traceback (most recent call last):
File "main.py", line 50, in <module>
File "main.py", line 44, in get_mqtt_client
File "umqtt/simple.py", line 99, in connect
MQTTException: 4
That's the board I am using:
https://www.amazon.com/gp/product/B0718T232Z/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1
That's the micropython version I am using:
esp32-idf3-20200902-v1.13.bin
and that's my code:
from third_party import rsa
import config
from third_party import string
from umqtt.simple import MQTTClient
from ubinascii import b2a_base64
import ujson
import utime
def on_message(topic, message):
print((topic,message))
def b42_urlsafe_encode(payload):
return string.translate(b2a_base64(payload)[:-1].decode('utf-8'),{ ord('+'):'-', ord('/'):'_' })
def create_jwt(project_id, private_key, algorithm, token_ttl):
print("Creating JWT...")
private_key = rsa.PrivateKey(*private_key)
# Epoch_offset is needed because micropython epoch is 2000-1-1 and unix is 1970-1-1. Adding 946684800 (30 years)
epoch_offset = 946684800
claims = {
# The time that the token was issued at
'iat': utime.time() + epoch_offset,
# The time the token expires.
'exp': utime.time() + epoch_offset + token_ttl,
# The audience field should always be set to the GCP project id.
'aud': project_id
}
#This only supports RS256 at this time.
header = { "alg": algorithm, "typ": "JWT" }
content = b42_urlsafe_encode(ujson.dumps(header).encode('utf-8'))
content = content + '.' + b42_urlsafe_encode(ujson.dumps(claims).encode('utf-8'))
signature = b42_urlsafe_encode(rsa.sign(content,private_key,'SHA-256'))
return content+ '.' + signature #signed JWT
def get_mqtt_client(project_id, cloud_region, registry_id, device_id, jwt):
"""Create our MQTT client. The client_id is a unique string that identifies
this device. For Google Cloud IoT Core, it must be in the format below."""
client_id = 'projects/{}/locations/{}/registries/{}/devices/{}'.format(project_id, cloud_region, registry_id, device_id)
print('Sending message with password {}'.format(jwt))
client = MQTTClient(client_id.encode('utf-8'),server=config.google_cloud_config['mqtt_bridge_hostname'],port=config.google_cloud_config['mqtt_bridge_port'],user=b'ignored',password=jwt.encode('utf-8'),ssl=True)
client.set_callback(on_message)
client.connect()
client.subscribe('/devices/{}/config'.format(device_id), 1)
client.subscribe('/devices/{}/commands/#'.format(device_id), 1)
return client
jwt = create_jwt(config.google_cloud_config['project_id'], config.jwt_config['private_key'], config.jwt_config['algorithm'], config.jwt_config['token_ttl'])
client = get_mqtt_client(config.google_cloud_config['project_id'], config.google_cloud_config['cloud_region'], config.google_cloud_config['registry_id'], config.google_cloud_config['device_id'], jwt)
I have checked my credentials to Google Cloud with other examples, and I am able to publish/receive messages with my computer, so it should be ok from that side.
As anyone encountered similar issues? Could you someone help on this please?
thx

How to fetch outlook calendar available time using python O365 library for schedule an appointment using aws lex -aws lambda function?

I'm a beginner to AWS and AZure. I'm doing a lex bot which will do a appointment based on the agent's outlook calendar.I head to fetch outlook calendar details (available time for appointment) and make an appointment on outlook calendar using python library O365. I'm having the client ID and secret key and an api to fetch the O365 calendar(Got it from the admin). I don't have a proper documentation about this O365 functionalities or methods.
from O365 import Account
credentials = ('my_client_id', 'my_client_secret')
account = Account(credentials)
if account.authenticate(scopes=['basic', 'message_all']):
print('Authenticated!')
When i ran this code, says me something like visit some URL and paste the API endpoint.
Questions:
1)I need to know the request type and format to call this api and calendar related functions in the O365(or a proper documentation to use this O365)
2)Will this O365 library can be imported inside the aws Lambda ? will it be pre-installed?
UPDATE:
I got tenant id and after using the own user identification method, now I'm able to authenticate. But After that when I'm doing this
from O365 import Account
import datetime as dt
credentials = ('xxxxx-0d57-49e5-xxxx-xxxxxxxx', 'xxxxx^[xxxxxxx[%')
account = Account(credentials, auth_flow_type='credentials', tenant_id='xxxxxx-e1ec-xxxxxxx-b1d9-xxxxxxxxx')
if account.authenticate():
print('Authenticated!')
schedule = account.schedule()
schedule.get_availability(['test#angkorwat.onmicrosoft.com'],dt.datetime(2019, 9, 5, 19, 45),dt.datetime(2019, 9, 10))
I'm getting the following error Resource not found for the segment 'calendar',
> C:\Users\MyPc\Desktop>python user_himself.py
> Authenticated! Client
> Error: 400 Client Error: Bad Request for url:
> https://graph.microsoft.com /v1.0/calendar/getSchedule | Error
> Message: Resource not found for the segment ' calendar'. Traceback
> (most recent call last): File "user_himself.py", line 13, in
> <module>
> schedule.get_availability(['test#angkorwat.onmicrosoft.com'],dt.datetime(
> 2019, 9, 5, 19, 45),dt.datetime(2019, 9, 10)) File
> "C:\Users\MyPc\AppData\Local\Programs\Python\Python37\lib\site-packages
> \O365\calendar.py", line 1931, in get_availability
> response = self.con.post(url, data=data) File "C:\Users\MyPc\AppData\Local\Programs\Python\Python37\lib\site-packages
> \O365\connection.py", line 725, in post
> return self.oauth_request(url, 'post', data=data, **kwargs) File "C:\Users\MyPc\AppData\Local\Programs\Python\Python37\lib\site-packages
> \O365\connection.py", line 703, in oauth_request
> return self._internal_request(self.session, url, method, **kwargs) File
> "C:\Users\Mypc\AppData\Local\Programs\Python\Python37\lib\site-packages
> \O365\connection.py", line 665, in _internal_request
> raise HTTPError('{} | Error Message: {}'.format(e.args[0], error_message), r esponse=response) from None
> requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:
> https://graph.microsoft.com/v1.0/calendar/getSchedule | Error
> Message: Resource not found for the segment 'calendar'.

Spotipy Refreshing Access Token Oauth2

I am trying to make a program that will remove songs from spotify playlists. Currently I am unable to refresh the auth tokens despite having them saved on my hard drive and specifying the cache paths when making oauth objects.
from bottle import route, run, request
import spotipy
from spotipy import oauth2
import time
PORT_NUMBER = 8080
SPOTIPY_CLIENT_ID = 'e1bb48ed8b594aeb9faf74f7e8915de7'
SPOTIPY_CLIENT_SECRET = 'dd6de8b7a0324d6ebf1fc0591e8e3220'
SPOTIPY_REDIRECT_URI = 'http://localhost:8080'
SCOPE = 'playlist-modify-public'
tracks = ['6DCZcSspjsKoFjzjrWoCdn']
def remove(sp_oauth):
access_token = ""
token_info = sp_oauth.get_cached_token()
if token_info:
if sp_oauth.is_token_expired:
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
access_token = token_info['access_token']
else:
print ("Found cached token!")
access_token = token_info['access_token']
if access_token:
print ("Access token available! Trying to get user information...")
sp = spotipy.Spotify(access_token)
results = sp.current_user_playlists()
userinfo = sp.current_user()
userid = userinfo['id']
for items in results['items']:
if userid == items['owner']['id']:
sp.user_playlist_remove_all_occurrences_of_tracks(items['owner']['id'],items['id'],tracks)
print("removed from " + items['owner']['display_name'] + " s list " + items['name'])
example =oauth2.SpotifyOAuth( SPOTIPY_CLIENT_ID, SPOTIPY_CLIENT_SECRET,SPOTIPY_REDIRECT_URI,scope=SCOPE,cache_path=r'C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\Caches\example')
remove(example)
The error I get is
Traceback (most recent call last):
File "C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\unfollowLists.py", line 212, in <module>
remove(blake)
File "C:\Users\rrako\AppData\Local\Programs\Python\Python38-32\unfollowLists.py", line 63, in remove
if sp_oauth.is_token_expired:
AttributeError: 'SpotifyOAuth' object has no attribute 'is_token_expired'
The version of spotipy I have is 2.4.4
Update your version of spotipy and the token should be refreshed automatically
pip3 install spotipy --upgrade

Google Cloud Pub Sub With Service Account

Hi everyone I am having an issue with pub sub that is driving me nuts. Basically I have a service account with admin priivs for pubsub but I can't get any thing to work and am getting the following error:
ERROR:root:AuthMetadataPluginCallback "" raised exception!
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/grpc/_plugin_wrapping.py", line 77, in call
callback_state, callback))
File "/usr/local/lib/python2.7/dist-packages/google/auth/transport/grpc.py", line 77, in call
callback(self._get_authorization_headers(context), None)
File "/usr/local/lib/python2.7/dist-packages/google/auth/transport/grpc.py", line 61, in _get_authorization_headers
self._credentials.before_request(
AttributeError: 'str' object has no attribute 'before_request'
Code is super simple
from google.cloud import pubsub
credentials = '/home/airflow/Desktop/test/config/test.json'
publisher = pubsub.PublisherClient(credentials=credentials)
topic_path = publisher.topic_path("test-proj", "test")
for n in range(1, 2):
data = u'Message number {}'.format(n)
# Data must be a bytestring
data = data.encode('utf-8')
test = publisher.publish(topic_path, data=data).result()
print(test, "s")
Amy help would be really appreciated as the error message doesn't make much sense to me. Thanks
The credentials argument for PublisherClient is not a string. It is a google.auth.credentials.Credentials object. The google-auth-guide indicates how to create it:
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'/home/airflow/Desktop/test/config/test.json')

Categories

Resources