Pesquisa de site

javascript - client_id e scope devem ser fornecidos juntos para inicializar o OAuth


Estou trabalhando com o Google Sheets API Javascript Quickstart e encontrei um problema de autenticação com gapi.client.init().

Aqui está o código que está me dando problemas:

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

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

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

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

Não consigo fazer a chamada init() funcionar, e recebo o erro como no título da pergunta mesmo se eu tentar chamar init() sozinho dentro do console:

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

O que está faltando na chamada para init()?

Qualquer dica seria apreciada.

Resposta #1

Eu também enfrentei o mesmo problema. Na função initClient eu substituí gapi.client.init por gapi.auth2.init e funcionou.