I have got an error on exercise 7/8


#1

I have done everything as told but I get error something like this,

watson_developer_cloud.watson_developer_cloud_service.WatsonException: Unauthorized: Access is denied due to invalid credentials

I checked all my credentials, and it is correct. Please help!
If you want to check my code,

import sys
import operator
import requests
import json
import twitter
from watson_developer_cloud import PersonalityInsightsV2 as PersonalityInsights

def analyze(handle):
    twitter_consumer_key = 'bpX1ANOf58u8NVgRvQXdzXU4N'
    twitter_consumer_secret = '7Vvw6AHd8xwRLOVPF2OuX3IoTp8fSv5IVxvTdFjerC4aYlZ5wy'
    twitter_access_token = '882381265-zCXkJtzHpzATrjecdlX3F5NNpT6pNeKbfWrXzyxf'
    twitter_access_secret = 'TNJy7sKDkCxJKIulZonNgmT6Hfa1mKpPzVfM0AirBTz1f'

    twitter_api = twitter.Api(consumer_key = twitter_consumer_key, 
                            consumer_secret =         twitter_consumer_secret,
                            access_token_key = twitter_access_token,
                            access_token_secret = twitter_access_secret)

    #handle = "@tim_cook"
    statuses = twitter_api.GetUserTimeline(screen_name = handle, count = 200, include_rts = False)

    text = ""
    for status in statuses:
        if(status.lang == 'en'):
            text += status.text.encode('utf-8')
        
    # IBM Watson Personality Insights credentials...
    pi_username = 'd8527f4c-031e-4723-92b3-a4a4a3853d91'
    pi_password = 'idtW2FZ6ksRY'

    personality_insights = PersonalityInsights(username = pi_username,
                                           password = pi_password)
    pi_result = personality_insights.profile(text)

    return pi_result

def flatten(orig):
    data = {}
    for c in orig['tree']['children']:
        if 'children' in c:
            for c2 in c['children']:
                if 'children' in c2:
                    for c3 in c2['children']:
                        if 'children' in c3:
                            for c4 in c3['childen']:
                                if (c4['category'] == 'personality'):
                                    data[c4['id']] = c4['percentage']
                                    if 'children' not in c3:
                                        if (c3['category'] == 'personality'):
                                            data[c3['id']] == c3['percentage']
                                            return data
def compare(dict1, dict2):
    compared_data = {}
    for keys in dict1:
        if dict1[keys] != dict2[keys]:
            compared_data[keys] = abs(dict1[keys] -
                                     dict2[keys])
            return data
          
user_handle = "@tim_cook"
celebrity_handle = "@pschiller"

user_result = analyze(user_handle)
celebrity_result = analyze(celebrity_handle)

user = flatten(user_result)
celebrity = flatten(celebrity_result)

compared_results = compare(user, celebrity)

sorted_result = sorted(compared_results.items(), key=operator.itemgetter(1))

for keys, value in sorted_result[:5]:
    print (keys),
    print(user[keys]),
    print ('->'),
    print (celebrity[keys]),
    print ('->'),
    print (compared_results[keys])

#2

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.