最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - client_id and scope must both be provided to initialize OAuth - Stack Overflow

programmeradmin5浏览0评论

I'm working off Google Sheets API Javascript Quickstart and have run into an authentication issue with gapi.client.init().

Here is the code that's giving me issues:

// Client ID and API key from the Developer Console
var CLIENT_ID = "50m3-cle3nt-1d.apps.googleusercontent";
var API_KEY = "AIzaN0tRe4lLyAn4pIk5y";

// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["/$discovery/rest?version=v4"];

// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = ".readonly";

function initClient() {
    gapi.client.init({
        apiKey: API_KEY,
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
    }).then(function() {
        // doesn't matter.
    })
}           

I can't get the init() call to work, and I get the error as in the question title even if I try to call init() on its own inside the console:

> k = gapi.client.init({ ... })
client_id and scope must both be provided to initialize OAuth

What am I missing in the call to init()?

Any tips would be appreciated.

I'm working off Google Sheets API Javascript Quickstart and have run into an authentication issue with gapi.client.init().

Here is the code that's giving me issues:

// Client ID and API key from the Developer Console
var CLIENT_ID = "50m3-cle3nt-1d.apps.googleusercontent.";
var API_KEY = "AIzaN0tRe4lLyAn4pIk5y";

// Array of API discovery doc URLs for APIs used by the quickstart
var DISCOVERY_DOCS = ["https://sheets.googleapis./$discovery/rest?version=v4"];

// Authorization scopes required by the API; multiple scopes can be
// included, separated by spaces.
var SCOPES = "https://www.googleapis./auth/spreadsheets.readonly";

function initClient() {
    gapi.client.init({
        apiKey: API_KEY,
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
    }).then(function() {
        // doesn't matter.
    })
}           

I can't get the init() call to work, and I get the error as in the question title even if I try to call init() on its own inside the console:

> k = gapi.client.init({ ... })
client_id and scope must both be provided to initialize OAuth

What am I missing in the call to init()?

Any tips would be appreciated.

Share Improve this question edited May 2, 2019 at 5:56 icedwater asked May 7, 2017 at 11:19 icedwatericedwater 4,8873 gold badges38 silver badges53 bronze badges 5
  • Update the value of var CLIENT_ID. To get the credentials, follow the Step 1 - d to i in this JavaScript Quickstart. You can not call init() on its own because it is dependent on this function handleClientLoad() which loads the auth2 library and API client library. – Mr.Rebot Commented May 7, 2017 at 17:24
  • I was using a valid CLIENT_ID, I just wasn't going to paste it here. But thanks for the note that init() will fail on its own. – icedwater Commented May 10, 2017 at 2:06
  • Anything new about this? I'm having the same problem with the Calendar API. I have my API Key and the web client ID. They are both correct. Also I have discoveryDocs in the init call :S – Osakr Commented Jan 23, 2019 at 12:44
  • Hmm, this isn't solved despite having simmered for 2 years. Am I missing something here? – icedwater Commented May 1, 2019 at 8:43
  • 1 I have the same problem with google calendar API – brian bentancourt Commented Sep 9, 2021 at 19:29
Add a ment  | 

1 Answer 1

Reset to default 6

I also faced the same problem. In the initClient function I replaced gapi.client.init to gapi.auth2.init and it worked.

发布评论

评论列表(0)

  1. 暂无评论