I am using IBM Watson for Sentiment Analysis and I get Forbidden Error 403. I contacted the IBM customer support mentioning the error and they asked me to check here.
import sys
import json
from ibm_watson import NaturalLanguageUnderstandingV1
from ibm_watson.natural_language_understanding_v1 import Features, SentimentOptions
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
authenticator = IAMAuthenticator('API Key')
natural_language_understanding = NaturalLanguageUnderstandingV1(version='2019-07 12',authenticator=authenticator)
response = natural_language_understanding.analyze(text='This is really Awesome', features = Features(sentiment=SentimentOptions())).get_result()
print(response)
The Error received is given below:
ERROR:root:Forbidden Traceback (most recent call last):
File "C:\filepath\filepath2\filepath3\ibm_cloud_sdk_core\base_service.py", line 229, in send response.status_code, error_message, http_response=response)
ibm_cloud_sdk_core.api_exception.ApiException: Error: Forbidden, Code: 403
Related
I'm trying to connect to use the Google Analytics API through AWS Lambda and then pull in data to put inside of an AWS RDS instance. I'm able to connect to my DB through Lambda, and the script works fine locally but when trying to run it on Lambda I get ModuleNotFoundError: No module named 'google.appengine'.
Haven't tried anything to solve this problem. Google searched the google appengine and its part of an SDK or something. Wondering what that means in terms of trying to connect to Lambda and how to proceed.
from __future__ import print_function
import argparse
import sys
import csv
from datetime import datetime
import pymysql
import googleapiclient.discovery
from googleapiclient.errors import HttpError
from googleapiclient import sample_tools
from oauth2client.client import AccessTokenRefreshError
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
def handler(event,context):
main(sys.argv)
def main(argv):
#Define the auth scopes to request.
scope = 'https://www.googleapis.com/auth/analytics.readonly'
key_file_location = 'serviceaccount.json'
#Authenticate and construct service.
service = get_service(
api_name='analytics',
api_version='v3',
scopes=[scope],
key_file_location=key_file_location)
#profile_id = profile_id = '######'
profile_id = profile_id = '########'
date, sessions = print_results(get_ga_metrics(service,profile_id))
write_to_sql(date,sessions)
def get_service(api_name, api_version, scopes, key_file_location):
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file_location, scopes=scopes)
service = build(api_name, api_version, credentials=credentials)
return service
def get_ga_metrics(service, profile_id):
"""Executes and returns data from the Core Reporting API.
This queries the API for the top 25 organic search terms by visits.
Args:
service: The service object built by the Google API Python client library.
profile_id: String The profile ID from which to retrieve analytics data.
Returns:
The response returned from the Core Reporting API.
"""
return service.data().ga().get(
ids='ga:' + profile_id,
start_date='30daysAgo',
end_date = 'yesterday',
metrics='ga:sessions',
dimensions='ga:date'
).execute()
def print_results(results):
date =[]
sessions =[]
for row in results.get('rows'):
date.append(row[0]) #loop thru new values in list to convert to datetime
sessions.append(row[1])
#dateformatted =[]
#for dates in date:
#date1 = datetime.strptime(dates,'%Y%m%d')
#dateformatted.append(date1)
dates = [datetime.strptime(x,'%Y%m%d').strftime('%Y-%m-%d') for x in date]
#print(dateformatted,sessions)
print(dates)
return dates,sessions
def write_to_sql(date,sessions):
db=pymysql.connect(host="################",user="###########",
passwd="#########",db="############")
cur = db.cursor()
all_list = list(zip(date,sessions))
sql = "INSERT INTO analytics (`date`,`sessions`) VALUES (%s,%s);"
cur.executemany(sql,all_list)
db.commit()
cur.close()
main(sys.argv)
This is what I get when I run the script locally.
['2019-04-22', '2019-04-23', '2019-04-24', '2019-04-25', '2019-04-26', '2019-04-27', '2019-04-28', '2019-04-29', '2019-04-30', '2019-05-01', '2019-05-02', '2019-05-03', '2019-05-04', '2019-05-05', '2019-05-06', '2019-05-07', '2019-05-08', '2019-05-09', '2019-05-10', '2019-05-11', '2019-05-12', '2019-05-13', '2019-05-14', '2019-05-15', '2019-05-16', '2019-05-17', '2019-05-18', '2019-05-19', '2019-05-20', '2019-05-21']
This is the error that AWS Lambda is throwing at me.
START RequestId: 33b53026-5c40-4040-8bba-8d4d58cdc553 Version: $LATEST
[WARNING] 2019-05-22T17:23:32.437Z file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/__init__.py", line 36, in autodetect
from google.appengine.api import memcache
ModuleNotFoundError: No module named 'google.appengine'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 33, in <module>
from oauth2client.contrib.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.contrib.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 37, in <module>
from oauth2client.locked_file import LockedFile
ModuleNotFoundError: No module named 'oauth2client.locked_file'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/task/googleapiclient/discovery_cache/__init__.py", line 41, in autodetect
from . import file_cache
File "/var/task/googleapiclient/discovery_cache/file_cache.py", line 41, in <module>
'file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth')
ImportError: file_cache is unavailable when using oauth2client >= 4.0.0 or google-auth
END RequestId: 33b53026-5c40-4040-8bba-8d4d58cdc553
REPORT RequestId: 33b53026-5c40-4040-8bba-8d4d58cdc553 Duration: 10011.13 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 30 MB
2019-05-22T17:23:39.538Z 33b53026-5c40-4040-8bba-8d4d58cdc553 Task timed out after 10.01 seconds
You need to turn off cache discovery when building your service:
service = build(
api_name,
api_version,
credentials=credentials,
cache_discovery=False,
)
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
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')
My tests file code:
from rest_framework import status
from rest_framework.test import APITestCase
class CategoryTests(APITestCase):
def test_create_create(self):
url = '/category/add/'
data = {"name":"Sports","description":"get live updates here"}
response = self.client.post(url, data, format='json')
self.assertEqual(response.data, data)
Error which I am getting:
Traceback (most recent call last):
File "/Users/test/webapp/apps/core/tests.py", line 16, in test_create_create
self.assertEqual(response.data, data)
AttributeError: 'HttpResponseNotAllowed' object has no attribute 'data'
Infact the tests are not even calling the exact api statements(I checked that using debug statements in api code). Please let me know what may be going wrong or you need any more information on this.
Try using the DRF extended test client:
from rest_framework import status
from rest_framework.test import APITestCase, APIClient
class CategoryTests(APITestCase):
client = APIClient()
def test_create_create(self):
url = '/category/add/'
data = {"name":"Sports","description":"get live updates here"}
response = self.client.post(url, data, format='json')
self.assertEquals(response.data, data)
Issue was with url, I correct it and it worked. So my url was actually
url = '/v1.0/category/add/'
I am new to python and I have been making codes to scrap twitter data on python.
Below are my codes:
import csv
import json
import twitter_oauth
import sys
sys.path.append("/Users/jdschnieder/Documents/Modules")
print sys.path
#gain authorization to twitter
consumer_key = 'xdbX1g21REs0MPxofJPcFw'
consumer_secret = 'c9S9YI6G3GIdjkg67ecBaCXSJDlGw4sybTv1ccnkrA'
get_oauth_obj = twitter_oauth.GetOauth(consumer_key, consumer_secret)
get_oauth_obj.get_oauth()
the error occurs at the line:
get_oauth_obj.get_oauth()
the error message is as follows:
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-36-5d124f99deb6> in <module>()
--> 1 get_oauth_obj.get_oauth()
/Users/jdschnieder/anaconda/python.app/Contents/lib/python2.7/site-packages/
twitter_oauth-0.2.0-py2.7.egg/twitter_oauth.pyc in get_oauth(self)
95 resp, content = client.request(_REQUEST_TOKEN_URL, "GET")
96 if resp['status'] != '200':
-> 97 raise Exception('Invalid response %s' % resp['status'])
98
99 request_token = dict(self._parse_qsl(content))
Exception: Invalid response 401
why is this error occurring, and what are possible solutions to the error?
thank you,
It looks like the Twitter library you're using does not support Twitter API v1.1. Instead of twitter_oauth, use one of the Python libraries listed here. For example, you can use Tweepy, following the documentation for OAuth Authentication