Discussions

Ask a Question
Back to All

Not able to use API call

I am using python with OpenIA integration and your API to generate the Video, after the call i am getting the error message Error: 401 - {"message":"Unauthorized"} and i have subscribe in you plan and and using API key that i have generated.

my sample code is


import openai
import requests

# Set your API keys
openai.api_key = "My OpenIA key removed "

DID_API_KEY = "My D-ID key removed"        # Replace with D-ID API key

# Function to generate text with OpenAI
def generate_script(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    script = response['choices'][0]['message']['content']
    print("Generated Script:", script)
    return script

# Function to create AI video with D-ID
def generate_ai_video(text, output_video_path="ai_video.mp4"):
    url = "https://api.d-id.com/talks"
    headers = {
        "Authorization": f"Bearer {DID_API_KEY}",
        "Content-Type": "application/json"
    }
    
    # Video generation request payload
    payload = {
        "script": {
            "type": "text",
            "input": text,
            "provider": {"type": "ai", "voice_id": "en-US-Standard-C"}
        },
        "source_url": "https://d-id-public-assets.s3.amazonaws.com/samples/DefaultVideoPresenter.mp4",
        "config": {
            "stitch": True
        }
    }
    
    # Make the API request
    response = requests.post(url, json=payload, headers=headers)
    if response.status_code != 201:
        print(f"Error: {response.status_code} - {response.text}")
        return None

    # Get the video URL from the response
    video_url = response.json()["result_url"]
    print(f"Video URL: {video_url}")

    # Download the video
    video_response = requests.get(video_url)
    with open(output_video_path, "wb") as f:
        f.write(video_response.content)
    print(f"Video saved as {output_video_path}")

# Main function
if __name__ == "__main__":
    # Generate text content using OpenAI
    user_prompt = "Write a 10-second introduction for an AI assistant video."
    generated_text = generate_script(user_prompt)

    # Generate a video from the text
    generate_ai_video(generated_text)

can you please help