什么是错误“ModuleNotFoundError: No module named 'google_auth_oauthlib'”以及如何在 Python 中解决它?

问题描述 投票:0回答:0

我正在开发一个涉及使用 google-auth-oauthlib 库的 Python 项目。但是,当我尝试从此库导入 InstalledAppFlow 时,出现以下错误:

vb网 复制代码 ModuleNotFoundError:没有名为“google_auth_oauthlib”的模块 我尝试使用 pip 安装库并以管理员身份运行命令,但我仍然遇到相同的错误。我在 Windows 10 上使用 Python 3.9。

任何人都可以帮我解决这个问题并成功地从 google-auth-oauthlib 导入 InstalledAppFlow 吗?谢谢。

import os
import datetime
import openai
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import httplib2
import requests
from bs4 import BeautifulSoup
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Setze hier deine API-Schlüssel und Anmeldeinformationen ein
YOUTUBE_API_KEY = 'YOUR_YOUTUBE_API_KEY'
OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY'
GMAIL_USER = 'YOUR_GMAIL_USER'
GMAIL_PASSWORD = 'YOUR_GMAIL_PASSWORD'

# Setze den OpenAI API-Schlüssel
openai.api_key = OPENAI_API_KEY

# YouTube API-Client erstellen
youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)

# Maximale Länge des generierten Texts (in Tokens)
MAX_TEXT_LENGTH = 4096


def get_top_videos(query):
    now = datetime.datetime.now()
    yesterday = now - datetime.timedelta(days=1)
    published_after = yesterday.isoformat("T") + "Z"

    search_response = youtube.search().list(
        q=query,
        part='id,snippet',
        maxResults=10,
        type='video',
        order='viewCount',
        publishedAfter=published_after,
        videoCaption='closedCaption',
    ).execute()

    videos = []
    for search_result in search_response.get('items', []):
        if search_result['id']['kind'] == 'youtube#video':
            videos.append((search_result['id']['videoId'], search_result['snippet']['title']))

    return videos


def extract_transcript(video_id):
    transcript_url = f'https://www.youtube.com/api/timedtext?v={video_id}&lang=en'
    response = requests.get(transcript_url)
    soup = BeautifulSoup(response.text, 'html.parser')

    transcript = ""
    for text in soup.find_all('text'):
        transcript += text.get_text(separator=' ') + ' '

    return transcript.strip()


def generate_personalized_text(title, transcript, max_tokens=100):
    prompt = f"PROMPT_TEXT_HERE"
    response = openai.Completion.create(
        engine="gpt-4",
        prompt=prompt,
        max_tokens=max_tokens,
        n=1,
        stop=None,
        temperature=0.8,
    )
    return response.choices[0].text.strip()


def send_email(to_email, subject, body):
    msg = MIMEMultipart()
    msg['From'] = GMAIL_USER
    msg['To'] = to_email
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))

    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(GMAIL_USER, GMAIL_PASSWORD)
    text = msg.as_string()
    server.sendmail(GMAIL_USER, to_email, text)
    server.quit()


if __name__ == '__main__':
    # Beispielaufrufe der Funktionen
    videos = get_top_videos('Snom')
    transcript = extract_transcript(videos[0][0])
    text = generate_personalized_text(videos[0][
python oauth google-oauth google-api-python-client modulenotfounderror
© www.soinside.com 2019 - 2024. All rights reserved.