"The request is missing a valid API key."

Am making a web application that one can read books through the site. I want to use google drive to store my PDF files for the textbooks, and in my application the Drive should serve the pdfs on my site when one want to read a book, I am using drive API, but I keep on getting the ("The request is missing a valid API key.") error, am using Django. when I check the logs for my local server it is working([31/Dec/2024 13:17:54] "GET /books/9/link/ HTTP/1.1" 200 96), but on the browser the pdf is not served what should I do?

enter code here

    >`
        
         drive/utils.py 
        from google.oauth2 import service_account 
        from googleapiclient.discovery import build 
        from payments.models  import Payment  
        from django.utils.timezone import now 
        from library.models import Book
        
            > # Path to your service account file SERVICE_ACCOUNT_FILE = 'BookShelf/credentials/vernal-segment-445921-k9-8c59294dc1a2.json'
            > SCOPES = ['https://www.googleapis.com/auth/drive.readonly']
            > 
            > def create_service(client_secret_file, api_name, api_version, scopes):
            >     """
            >     Create and return a Google API service instance.
            >     """
            >     credentials = service_account.Credentials.from_service_account_file(
            >         client_secret_file, scopes=scopes
            >     )
            >     return build(api_name, api_version, credentials=credentials)
            > 
            > def initialize_drive_service():
            >     """
            >     Initialize and return the Google Drive service.
            >     """
            >     return create_service(SERVICE_ACCOUNT_FILE, 'drive', 'v3', SCOPES)
            > 
            > def generate_pdf_link(file_id):
            >     """
            >     Generate a shareable link for a Google Drive file.
            >     :param file_id: The ID of the file on Google Drive
            >     :return: A direct download URL
            >     """
            >     url = f"https://www.googleapis.com/drive/v3/files/{file_id}?alt=media"
            >     return url
            > 
            > def set_file_permissions(file_id, role='reader', type_='anyone'):
            >     """
            >     Set permissions for a Google Drive file.
            >     :param file_id: The ID of the file on Google Drive
            >     :param role: The role to assign (e.g., 'reader', 'writer')
            >     :param type_: The type of user to grant access (e.g., 'user', 'anyone')
            >     """
            >     drive_service = initialize_drive_service()
            >     permission_body = {'role': role, 'type': type_}
            >     drive_service.permissions().create(fileId=file_id, body=permission_body).execute()
            > 
            > def get_shareable_link(file_id):
            >     """
            >     Retrieve a shareable link for a Google Drive file.
            >     :param file_id: The ID of the file on Google Drive
            >     :return: The shareable link
            >     """
            >     drive_service = initialize_drive_service()
            >     response = drive_service.files().get(fileId=file_id, fields='webViewLink').execute()
            >     return response.get('webViewLink')
            > 
            > 
views.py
 @login_required 
       def open_book(request, book_id):
            >     try:
            >         book = Book.objects.get(id=book_id)
            >     except Book.DoesNotExist:
            >         return HttpResponseNotFound("Book not found.")

        
            >      file_id = book.drive_file_id
            >     pdf_url = generate_pdf_link(file_id)  
            >     return render(request, 'library/opn-book2.html', {'pdf_url': pdf_url, 'book': book})
            > 
            > url.py urls = [    
path('books/<int:book_id>/link/',views.open_book,name='open_book'), ]
            > 
            > 
    my logs 
     [31/Dec/2024 13:17:54] "GET /books/9/link/ HTTP/1.1" 200 96`
Вернуться на верх