openai / openai-node

Official JavaScript / TypeScript library for the OpenAI API
https://www.npmjs.com/package/openai
Apache License 2.0
7.93k stars 862 forks source link

[Error: 401 Missing bearer or basic authentication in header] - React-Native #756

Closed Mykeb96 closed 7 months ago

Mykeb96 commented 7 months ago

Confirm this is a Node library issue and not an underlying OpenAI API issue

Describe the bug

I am using the exact same setup that I use in my web app, as I am in my react-native project. The exact same api key, and assistant ID are being used between the two apps. I am able to retrieve my assistant no problem in the web app, but get [Error: 401 Missing bearer or basic authentication in header] in my react-native apps.

To Reproduce

  1. Create new react-native project
  2. Run ios simulator through xCode
  3. import openai
  4. create new OpenAI client
  5. attempt to retrieve assistant

Code snippets

import { OpenAI } from 'openai'

const openai = new OpenAI({
    apiKey: 'my-key',
    dangerouslyAllowBrowser: true,
    organization: 'my-org'
})

openai.beta.assistants.retrieve('assistant-id')
.then(res => console.log(res))
.catch(err => console.log(err))

OS

macOS

Node version

node v21.7.1

Library version

openai ^4.31.0

rattrayalex commented 7 months ago

As noted in the README, React Native is not supported. We don't plan to add built-in support for React Native, but it does seem that most features (apart from file uploads) work with a polyfill. Are you using one?

Note that in general, you should plan to call OpenAI APIs only from your backend, to avoid exposing your API keys.

Mykeb96 commented 7 months ago

As noted in the README, React Native is not supported. We don't plan to add built-in support for React Native, but it does seem that most features (apart from file uploads) work with a polyfill. Are you using one?

Note that in general, you should plan to call OpenAI APIs only from your backend, to avoid exposing your API keys.

I found a polyfill that works actually. thanks for the suggestion!