I am trying to get all assignments from a users classes and for some reason, it gives me this error:
Traceback (most recent call last):
File "C:/Users/oscar/Desktop/Development/Python/main.py", line 50, in <module>
main()
File "C:/Users/oscar/Desktop/Development/Python/main.py", line 29, in main
creds = flow.run_local_server(port=0)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 474, in run_local_server
self.fetch_token(authorization_response=authorization_response)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\google_auth_oauthlib\flow.py", line 288, in fetch_token
return self.oauth2session.fetch_token(self.client_config["token_uri"], **kwargs)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\requests_oauthlib\oauth2_session.py", line 360, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 421, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 431, in parse_token_response
validate_token_parameters(params)
File "C:\Users\oscar\Desktop\Development\Python\venv\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 461, in validate_token_parameters
raise w
Warning: Scope has changed from "https://www.googleapis.com/auth/classroom.courses.readonly https://www.googleapis.com/auth/classroom.coursework.me.readonly" to "https://www.googleapis.com/auth/classroom.student-submissions.me.readonly https://www.googleapis.com/auth/classroom.courses.readonly".
Process finished with exit code 1
Here is my code:
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/classroom.courses.readonly', 'https://www.googleapis.com/auth/classroom.coursework.me.readonly']
def main():
"""Shows basic usage of the Classroom API.
Prints the names of the first 10 courses the user has access to.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('classroom', 'v1', credentials=creds)
# Call the Classroom API
results = service.courses().list(pageSize=10).execute()
courses = results.get('courses', [])
if not courses:
print('No courses found.')
else:
for course in courses:
print(course['name'])
course_id = course['id']
results1 = service.courses().courseWork().list(courseId=course_id).execute()
print(results1)
if __name__ == '__main__':
main()
I think this is happening because of the second scope but it's the one they say to use on the official API so Idk why its giving me an error. Any help would be appreciated.
Related
so I have just started getting interested in creating discord bots for my own server and mostly for fun, so I'm a a bit of a beginner in that, and this time I'm trying to create a discord bot that logs movies I have watched with my friends and also let's me search through the list of movies, delete a movie or if i just want to list the movies, and I'm trying to use davinci-003 model from the openai API to extract the movie detail that I have added, in case i give it an incomplete movie name so it would complete it for me, or so it would add the movie year and extra details about the movie I'm trying to add, at least that was my intention.
however first of all the very first issue is my bot doesn't show any status when i run it (i.e say watching a movie in the status of the bot), and also none of the / commands work on discord, they all return "The application did not respond" on discord and I get this error in the terminal where the code is running:
when I use /add moviename
Task exception was never retrieved
future: <Task finished name='Task-19' coro=<add() done, defined at /usr/local/lib/python3.10/dist-packages/interactions/client/models/command.py:930> exception=TypeError("add() missing 1 required positional argument: 'movie_details'")>
Traceback (most recent call last):
File "/usr/local/lib/python3.10/dist-packages/interactions/client/models/command.py", line 970, in wrapper
raise e
File "/usr/local/lib/python3.10/dist-packages/interactions/client/models/command.py", line 939, in wrapper
return await coro(ctx, *args, **kwargs)
TypeError: add() missing 1 required positional argument: 'movie_details'
and same error for /delete moviename and /search moviename with their own functions though (i.e delete() or search() which is missing 1 required positional argument: 'query'), for /list however, on discord I still get "The application did not respond" even though it's just supposed to return my empty movie list.
and here is my code:
import openai
import discord
import os
import interactions
from dotenv import load_dotenv
# Load environment variables from .env file
try:
load_dotenv()
except FileNotFoundError:
print("Could not find .env file")
openai.api_key = os.getenv("API_KEY")
if openai.api_key is None:
print("API key not set. Set the API key using openai.api_key = 'YOUR_API_KEY'")
else:
print("Connected to OpenAI API key...")
# Create a bot object using the interactions library
bot = interactions.Client(
token=os.getenv("DISCORD_BOT_TOKEN"),
activity=discord.Activity(type=discord.ActivityType.watching, name="a Movie")
)
# This function is used to get the list of movies from the logger (by reading from the movie-log.txt file)
def get_movie_list():
movie_list = []
try:
with open("movie-log.txt", "r") as movie_log_file:
for line in movie_log_file:
movie_list.append(line.strip())
except FileNotFoundError:
return "movie-log.txt file not found"
else:
return "\n".join(movie_list)
# This function is used to search for movies in the logger (by searching the movie-log.txt file)
def search_movies(query):
search_results = []
try:
with open("movie-log.txt", "r") as movie_log_file:
for line in movie_log_file:
if query.lower() in line.lower():
search_results.append(line.strip())
except FileNotFoundError:
return "movie-log.txt file not found"
else:
if not search_results:
return "No results found"
return "\n".join(search_results)
# This function is used to add a movie to the logger (by appending to the movie-log.txt file)
def add_movie(movie_details):
if not movie_details:
return
# Check if the movie already exists in the logger
if movie_details in search_movies(movie_details):
return
try:
with open("movie-log.txt", "a") as movie_log_file:
movie_log_file.write(f"{movie_details}\n")
except FileNotFoundError:
return "movie-log.txt file not found"
# This function is used to delete a movie from the logger (by overwriting the movie-log.txt file)
def delete_movie(movie_details):
movie_list = get_movie_list()
if movie_details not in movie_list:
return "Movie not found"
try:
with open("movie-log.txt", "w") as movie_log_file:
for movie in movie_list:
if movie != movie_details:
movie_log_file.write(f"{movie}\n")
except FileNotFoundError:
return "movie-log.txt file not found"
# This function is used to handle errors
def handle_error(error):
try:
return f"Error of type {type(error)} occurred: {error}"
except Exception as e:
return "Unable to create error message"
# Debug intents
debug_guild = int(os.getenv("DEBUG_GUILD"))
debug_channel = int(os.getenv("DEBUG_CHANNEL"))
# Define the on_ready event handler
#bot.event
async def on_ready():
print("Bot is up and running...")
print("Bot is ready to use!")
# Define the /add command using the command decorator
#bot.command(
name="add",
description="Adds a movie to the movie log",
scope=debug_guild,
)
async def add(ctx: interactions.CommandContext, movie_details: str):
try:
response = openai.Completion.create(
engine="davinci-003",
prompt=f"/add {movie_details}",
max_tokens=1024,
temperature=0.5,
)
except Exception as e:
# Handle any errors that occurred
await ctx.send(handle_error(e))
else:
# Add the movie to the logger
result = add_movie(response.text.strip())
if result:
await ctx.send(result)
# Define the /delete command using the command decorator
#bot.command(
name="delete",
description="Deletes a movie from the movie log",
scope=debug_guild,
)
async def delete(ctx: interactions.CommandContext, movie_details: str):
try:
response = openai.Completion.create(
engine="davinci-003",
prompt=f"/delete {movie_details}",
max_tokens=1024,
temperature=0.5,
)
except Exception as e:
# Handle any errors that occurred
await ctx.send(handle_error(e))
else:
# Delete the movie from the logger
result = delete_movie(response.text.strip())
if result:
await ctx.send(result)
# Define the /search command using the command decorator
#bot.command(
name="search",
description="Searches for movies in the movie log",
scope=debug_guild,
)
async def search(ctx: interactions.CommandContext, query: str):
# Search for movies in the logger
result = search_movies(query)
if result:
await ctx.send(result)
# Define the /list command using the command decorator
#bot.command(
name="list",
description="Lists all movies in the movie log",
scope=debug_guild,
)
async def list(ctx: interactions.CommandContext):
# Get the list of movies from the logger
result = get_movie_list()
if result:
await ctx.send(result)
# Handle command errors using the on_command_error event
#bot.event
async def command_error(ctx, error):
# Send an error message if a CommandNotFound error occurred
if isinstance(error, interactions.CommandNotFound):
await ctx.send("Command not found")
# Run the bot
bot.start()
Please let me know if anything is unclear or if I don't make sense ;-; Thank you!
What I've tried:
Well, at first I was using commands from discord.ext library but then none of my / commands were even showing up on discord, and I searched the web and found out that it's outdated and that I should switch to using interactions library, I used the docs from https://discord-py-slash-command.readthedocs.io/ to switch to interactions, my / commands started showing up when tried to call on discord however I don't know if I coded them correctly.
I'm trying to upload videos to youtube using Django and MSSQL, I want to store the user data to DB so that I can log in from multiple accounts and post videos.
The official documentation provided by youtube implements a file system and after login, all the user data gets saved there, I don't want to store any data in a file as saving files to DB would be a huge risk and not a good practice. So how can I bypass this step and save data directly to DB and retrieve it when I want to post videos to a specific account?
In short, I want to replace the pickle file implementation with storing it in the database.
Here's my code
def youtubeAuthenticate():
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "client_secrets.json"
creds = None
# the file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# if there are no (valid) credentials availablle, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(client_secrets_file, SCOPES)
creds = flow.run_local_server(port=0)
# save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
return build(api_service_name, api_version, credentials=creds)
#api_view(['GET','POST'])
def postVideoYT(request):
youtube = youtubeAuthenticate()
print('yt',youtube)
try:
initialize_upload(youtube, request.data)
except HttpError as e:
print("An HTTP error %d occurred:\n%s" % (e.resp.status, e.content))
return Response("Hello")
def initialize_upload(youtube, options):
print('options', options)
print("title", options['title'])
# tags = None
# if options.keywords:
# tags = options.keywords.split(",")
body=dict(
snippet=dict(
title=options['title'],
description=options['description'],
tags=options['keywords'],
categoryId=options['categoryId']
),
status=dict(
privacyStatus=options['privacyStatus']
)
)
# # Call the API's videos.insert method to create and upload the video.
insert_request = youtube.videos().insert(
part=",".join(body.keys()),
body=body,
media_body=MediaFileUpload(options['file'], chunksize=-1, resumable=True)
)
path = pathlib.Path(options['file'])
ext = path.suffix
getSize = os.path.getsize(options['file'])
resumable_upload(insert_request,ext,getSize)
# This method implements an exponential backoff strategy to resume a
# failed upload.
def resumable_upload(insert_request, ext, getSize):
response = None
error = None
retry = 0
while response is None:
try:
print("Uploading file...")
status, response = insert_request.next_chunk()
if response is not None:
respData = response
if 'id' in response:
print("Video id '%s' was successfully uploaded." % response['id'])
else:
exit("The upload failed with an unexpected response: %s" % response)
except HttpError as e:
if e.resp.status in RETRIABLE_STATUS_CODES:
error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
e.content)
else:
raise
except RETRIABLE_EXCEPTIONS as e:
error = "A retriable error occurred: %s" % e
if error is not None:
print(error)
retry += 1
if retry > MAX_RETRIES:
exit("No longer attempting to retry.")
max_sleep = 2 ** retry
sleep_seconds = random.random() * max_sleep
print("Sleeping %f seconds and then retrying..." % sleep_seconds)
time.sleep(sleep_seconds)
I'm looking for a Google API to get the size of the drive, but I can't find anything. The code to delete a user's email using the Google API is provided below. Similarly, I need to know the size of the user's drive. Could someone please assist me? Is there a way to get the drive's size via an API? Thanks.
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/admin.directory.group', 'https://www.googleapis.com/auth/admin.directory.user']
def main():
"""Shows basic usage of the Admin SDK Directory API.
Prints the emails and names of the first 10 users in the domain.
"""
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
service = build('admin', 'directory_v1', credentials=creds)
return service
def test():
# user = service.users().get(userKey="user1#matador.csun.edu").execute()
# members = service.groups().list(domain='my.csun.edu', userKey=user['primaryEmail'], pageToken=None, maxResults=500).execute()
# print(user)
# Call the Admin SDK Directory API
print('Getting the first 10 users in the domain')
results = service.users().list(customer='my_customer', maxResults=10,
orderBy='email').execute()
print(results)
users = results.get('users', [])
if not users:
print('No users in the domain.')
else:
print('Users:')
for user in users:
print(user)
# print(dir(user))
# print(u'{0} ({1})'.format(user['primaryEmail'],
# user['name']['fullName']))
def del_user(user):
try:
service.users().delete(userKey=user).execute()
print("Deleted!")
except:
print("User doesn't exist!")
if __name__ == '__main__':
service = main()
nameExt='23'
# with open('NewGmailInProd/gmailUser'+nameExt+'.txt') as fileToRead:
# with open('NewGmailInProd/test.txt') as fileToRead:
# emails = fileToRead.readlines()
emails = ['user1#matador.csun.edu']
for email in emails:
del_user(email.strip())
The google drive api has an endpoint called about.get This endpoint returns a lot of information about a users drive account try me
One of the things it returns is the users storage quota.
You appear to be trying to go though the Admin SDK Directory API it just gives you access to administer Workspace its not going to give you anything with drive
How to make Api anime character database function with my discord python bot? I am new add discord python bot. This sample api link for anime character database:
https://www.animecharactersdatabase.com/api_series_characters.php?character_q=naruto
I think this should work, try this! i commented out some stuff for it to be easy to understand
# Requrired modules
import discord
from discord.ext import commands
import requests
import random
import platform
# Creating the bot with the bot prefix as `?`
client = commands.Bot(command_prefix="?")
#client.event
async def on_ready():
print(f'Discord.py API version: {discord.__version__}')
print(f'Python version: {platform.python_version()}')
print(f'Logged in as {client.user.name}')
print('Bot is ready!')
# The commandsstarts here
#client.command()
async def anime(ctx, *, query="naruto"): # 'anime' is the name of the command
# This command can be used as `?anime character_name_here`
# The default value will be 'naruto' for the query
try:
reqcont = requests.get(f"https://www.animecharactersdatabase.com/api_series_characters.php?character_q={query}")
if reqcont.content==-1 or reqcont.content=='-1': # i found out that the website returns: -1 if there are no results, so here, we implement it
await ctx.send("[-] Unable to find results! - No such results exists!")
else:
# If the website doesnt return: -1 , this will happen
try:
reqcont = reqcont.json()
except Exception as e:
# Please enable this line only while you are developing and not when deplying
await ctx.send(reqcont.content)
await ctx.send(f"[-] Unable to turn the data to json format! {e}")
return # the function will end if an error happens in creating a json out of the request
# selecting a random item for the output
rand_val = len(reqcont["search_results"])-1
get_index = random.randint(0, rand_val)
curent_info = reqcont["search_results"][get_index]
# Creting the embed and sending it
embed=discord.Embed(title="Anime Info", description=":smiley: Anime Character Info result for {query}", color=0x00f549)
embed.set_author(name="YourBot", icon_url="https://cdn.discordapp.com/attachments/877796755234783273/879295069834850324/Avatar.png")
embed.set_thumbnail(url=f"{curent_info['anime_image']}")
embed.set_image(url=f"{curent_info['character_image']}")
embed.add_field(name="Anime Name", value=f"{curent_info['anime_name']}", inline=False)
embed.add_field(name="Name", value=f"{curent_info['name']}", inline=False)
embed.add_field(name="Gender", value=f"{curent_info['gender']}", inline=False)
embed.add_field(name="Description", value=f"{curent_info['desc']}", inline=False)
embed.set_footer(text=f"Requested by {ctx.author.mention}")
await ctx.send(embed=embed)
except Exception as e:
await ctx.send(f"[-] An error has occured: {e}")
client.run("NeverShareYourToken")
I am trying to make a small API that logs into a internal monitoring tool via web and retrieves images on pages that I specify using the login credentials I specify. It's not passing any authentication to the last section after it has already built the URL. After the URL is built, it is put into variable y. I then attempt to open y and save it, that is where the authentication problem is happening. Please scroll down for examples.
import urllib
import urllib2
import lxml, lxml.html
ID = raw_input("Enter ID:")
PorS = raw_input("Enter 1 for Primary 2 for Secondary:")
base_link = 'http://statseeker/cgi/nim-report?rid=42237&command=Graph&mode=ping&list=jc-'
dash = '-'
end_link = '&tfc_fav=range+%3D+start_of_today+-+1d+to+now%3B&year=&month=&day=&hour=&minute=&duration=&wday_from=&wday_to=&time_from=&time_to=&tz=America%2FChicago&tfc=range+%3D+start_of_today+-+1d+to+now%3B&rtype=Delay&graph_type=Filled&db_type=Average&x_step=1&interval=60&y_height=100&y_gridlines=5&y_max=&y_max_power=1&x_gridlines=on&legend=on'
urlauth = base_link + jConnectID + dash + jConnectPorS + end_link
print urlauth
realm = 'statseeker'
username = 'admin'
password = '*****'
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm, urlauth, username, password)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
data = opener.open(urlauth).read()
html = lxml.html.fromstring(data)
imgs = html.cssselect('img.graph')
for x in imgs:
y = 'http://statseeker%s' % (x.attrib['src'])
g = urllib2.urlopen(y).read()
urllib2.urlopen(test.jpg, 'wb').write(g)
print 'http://statseeker%s' % (x.attrib['src'])
with open('statseeker.html', 'a') as f:
f.write(y)
Result:
C:\Users\user\Documents\Scripting>python test.py
Enter ID:4050
Enter 1 for Primary 2 for Secondary:1
http://statseeker/cgi/nim-report?rid=42237&command=Graph&mode=ping&list=jc-4050-
1&tfc_fav=range+%3D+start_of_today+-+1d+to+now%3B&year=&month=&day=&hour=&minute
=&duration=&wday_from=&wday_to=&time_from=&time_to=&tz=America%2FChicago&tfc=ran
ge+%3D+start_of_today+-+1d+to+now%3B&rtype=Delay&graph_type=Filled&db_type=Avera
ge&x_step=1&interval=60&y_height=100&y_gridlines=5&y_max=&y_max_power=1&x_gridli
nes=on&legend=on
Traceback (most recent call last):
File "JacksonShowAndSave.py", line 35, in <module>
g = urllib2.urlopen(y).read()
File "C:\Python27\lib\urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 410, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 448, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 401: Authorization Required
C:\Users\user\Documents\Scripting>
How do I fix these errors to pass the Authentication to any web page opened on that website? I thought that any urlopen request would use the same authentication as above.
The Section that is Failing:
y = 'http://statseeker%s' % (x.attrib['src'])
g = urllib2.urlopen(y).read()
urllib2.urlopen(test.jpg, 'wb').write(g)
print 'http://statseeker%s' % (x.attrib['src'])
with open('statseeker.html', 'a') as f:
I built another auth handler for the request:
auth_handler2 = urllib2.HTTPBasicAuthHandler()
auth_handler2.add_password(realm, y, username, password)
opener2 = urllib2.build_opener(auth_handler2)
urllib2.install_opener(opener2)
link = urllib2.Request(y)
response = urllib2.urlopen(link)
output = open('out2.jpg','wb')
output.write(response.read())
output.close()