2017-04-15 7 views
0

다음 코드에 대한 인증이 실패하고 이유를 파악할 수 없습니다. 어떤 도움을 주시면 감사하겠습니다.Gmail API 인증이 작동하지 않음 (javascript)

먼저 관련 HTML script :

<script src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> 

이제 자바 스크립트 :

var clientId = "xxxxxxxx"; 
var apiKey = "xxxxxxxx"; 
var scopes = "https://www.googleapis.com/auth/gmail.send"; 

function handleClientLoad() { 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth, 1); 
} 
function checkAuth() { 
    gapi.auth.authorize({ 
     client_id: clientId, 
     scope: scopes, 
     immediate: true 
    }, handleAuthResult); 
} 
function handleAuthResult(authResult) { 
    if (authResult && !authResult.error) { 
     loadGmailApi(); 
     console.log("gmail authentication passed"); 
    } else { 
     console.log("gmail authentication failed"); 
    } 
} 
function loadGmailApi() { 
    gapi.client.load("gmail", "v1", close); 
} 
function close() { 
    return false; 
} 

답변

0

것은이 문서에 대한 Authentication using the Google APIs Client Library for JavaScript을 따라 있는지 확인합니다.

Gmail API에 대한 모든 요청은 인증 된 사용자의 승인을 받아야합니다. Gmail은 OAuth 2.0 protocol을 사용하여 Google 계정을 인증하고 사용자 데이터에 대한 액세스 권한을 부여합니다. Google+ Sign-in을 사용하여 앱에 대해 "Google 로그인"인증 방법을 제공 할 수도 있습니다.

또한 여기에 JavaScript Quickstart이 도움이 될 수 있습니다.

// Authorization scopes required by the API; multiple scopes can be included, separated by spaces. 
     var SCOPES = 'https://www.googleapis.com/auth/gmail.readonly';  
     var authorizeButton = document.getElementById('authorize-button'); 
     var signoutButton = document.getElementById('signout-button'); 

/** 
* On load, called to load the auth2 library and API client library. 
*/ 
     function handleClientLoad() { 
     gapi.load('client:auth2', initClient); 
     } 

희망이 있습니다.

0

이 문제로 너무 오래 고생하고 나자 마침내 웹 호스팅 제공 업체의 전자 메일 서버를 사용하여 서버 측에서 PHP를 사용하는 접근 방식을 채택하기로 결정했습니다. 따라서 이것이 작동하지 않는 이유에 대해 답변을 드릴 수는 없지만 서버 측을 사용하여 전자 메일을 보내도록 권장 할 수 있습니다.