I can't work with GMail API. Each time GMail cannot be read and is an empty property. I organize myself, I can use the API of YouTube, but I cannot achieve the use of Google api. Help please make out my mistake.

I cite the code:

var API_KEY = 'AIzaSyBnm60DnSJ2xVsRjo-L_9Mx_mALPy4ttUY'; var OAUTH2_CLIENT_ID = '435367929011-rutrvfe1hp1c85ra9a7vhnmtk3q72afa.apps.googleusercontent.com'; var OAUTH2_SCOPES = [ 'https://www.googleapis.com/auth/youtube', 'https://www.googleapis.com/auth/gmail.readonly' ]; var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"]; var GoogleAuth; var SCOPE = 'https://www.googleapis.com/auth/youtube.force-ssl'; function handleClientLoad() { // Load the API's client and auth2 modules. // Call the initClient function after the modules load. gapi.load('client:auth2', initClient); } function initClient() { // Retrieve the discovery document for version 3 of YouTube Data API. // In practice, your app can retrieve one or more discovery documents. var discoveryUrl = 'https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'; // Initialize the gapi.client object, which app uses to make API requests. // Get API key and client ID from API Console. // 'scope' field specifies space-delimited list of access scopes. gapi.client.init({ 'apiKey': API_KEY, 'discoveryDocs': [discoveryUrl], 'clientId': OAUTH2_CLIENT_ID, 'scope': SCOPE }).then(function () { GoogleAuth = gapi.auth2.getAuthInstance(); // Listen for sign-in state changes. GoogleAuth.isSignedIn.listen(updateSigninStatus); // Handle initial sign-in state. (Determine if user is already signed in.) var user = GoogleAuth.currentUser.get(); setSigninStatus(); // Call handleAuthClick function when user clicks on // "Sign In/Authorize" button. $('#sign-in-or-out-button').click(function() { handleAuthClick(); }); $('#revoke-access-button').click(function() { revokeAccess(); }); }); } function handleAuthClick() { if (GoogleAuth.isSignedIn.get()) { // User is authorized and has clicked 'Sign out' button. GoogleAuth.signOut(); } else { // User is not signed in. Start Google auth flow. GoogleAuth.signIn(); } } function revokeAccess() { GoogleAuth.disconnect(); } function setSigninStatus(isSignedIn) { var isAuthorized = user.hasGrantedScopes(SCOPE); if (isAuthorized) { $('#sign-in-or-out-button').html('Sign out'); $('#revoke-access-button').css('display', 'inline-block'); $('#auth-status').html('You are currently signed in and have granted ' + 'access to this app.'); } else { $('#sign-in-or-out-button').html('Sign In/Authorize'); $('#revoke-access-button').css('display', 'none'); $('#auth-status').html('You have not authorized this app or you are ' + 'signed out.'); } gapi.client.gmail.users; } function updateSigninStatus(isSignedIn) { setSigninStatus(); } function gmailInit() { GoogleAuth.currentUser.get(); gapi.client.init({ 'discoveryDocs': ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"], 'clientId': OAUTH2_CLIENT_ID, 'scope': "https://www.googleapis.com/auth/gmail.readonly", 'apiKey': API_KEY }).then(function(){ gapi.client.gmail.Users.getProfile({ 'userId': 'me' }).then(function(response) { var labels = response.result.labels; appendPre('Labels:'); if (labels && labels.length > 0) { for (i = 0; i < labels.length; i++) { var label = labels[i]; appendPre(label.name) } } else { appendPre('No Labels found.'); } }); }); } $(window).load(function() { $('#gmail-init-button').on('click',function() { gmailInit(); }); }); 

    1 answer 1

    Your GoogleAuth variable GoogleAuth initialized in the initClient() function, but there is no call to this function.

     $(function() { gapi.load('client', initClient); $('#gmail-init-button').on('click', function() { gmailInit(); }); }); 
    • Thanks for your reply! I will try this code and write to you right away. - Ivan Hanenko
    • Excuse me. That line remained experiments. The GMail field is always empty. That is the problem. - Ivan Hanenko
    • one
      @Ivanganenko, provide the current code. - Alex Zhulin
    • I solved the question. The addition of the discoveryDocs field helped. Now it looks like this - 'discoveryDocs': [discoveryUrl, " googleapis.com/discovery/v1/apis/gmail/v1/rest"] . Thank you for the answer and since you are the one who responded, I will mark yours as correct. - Ivan Hanenko
    • one
      @ Ivanganenko, yes. - Alex Zhulin