2017-04-19 7 views
0

요청 모듈을 특별히 사용해야합니다. 새로 고침 토큰과 액세스 토큰이 있습니다.Gmail 계정의 스레드에 액세스하기 위해 nodejs "request"라이브러리를 사용하려면 어떻게해야합니까?

request({ 
      method: "GET", 
      uri:"https://www.googleapis.com/gmail/v1/users/me/threads", 
      headers: { 
       "access_token": 'access_token', 
       "refresh_token": 'refresh_token', 
       "token_type": 'Bearer', 
       "Content-Type": "application/json" 
      }, 
     }, 
     function(err, response, body) { 
      if(err){ 
       console.log(err); // Failure 
      } else { 
       console.log(response); 
       // done(null);// Success! 
      } 
     }); 

로그인 할 때마다 401 오류가 발생합니다. 또한 어떻게 특정 쿼리 "q"를 사용하여 요청과 함께 보낼 수 있습니까?

답변

0

다음 샘플은 어떻습니까?

이것을 사용하려면 범위에 https://mail.google.com/을 포함하고 Google API 콘솔에서 Gmail API를 사용하도록 설정하세요. 이미 수행했다면 무시하십시오.

또한 "q"를 스크립트에 포함 시켰습니다. 세부 정보는 https://developers.google.com/gmail/api/v1/reference/users/threads/list입니다. 확인해주세요.

스크립트 :

var request = require('request'); 
request({ 
     url: 'https://www.googleapis.com/gmail/v1/users/me/threads', 
     method: 'GET', 
     headers: { 
      "Content-Type": "application/json", 
      "Authorization": "Bearer ### your access token ###" 
     }, 
     qs: { 
      "q": "from:### mail address ###", // For example, a filter is added by ``from``. 
      "fields": "threads" 
     } 
    }, 
    function (err, response, body) { 
     if(err){ 
      console.log(err); // Failure 
     } else { 
      console.log(body); 
      // done(null);// Success! 
    } 
}); 
+0

감사합니다. 내게 도움이되었습니다. – Avy

+0

환영합니다. 고마워. – Tanaike

0

NodeJS Quickstart for Gmail을 따르십시오. 당신이 Users.messages.list 또는 Users.threads.list 전화를 걸 때 'Q'를 사용

function authorize(credentials, callback) { 
    var clientSecret = credentials.installed.client_secret; 
    var clientId = credentials.installed.client_id; 
    var redirectUrl = credentials.installed.redirect_uris[0]; 
    var auth = new googleAuth(); 
    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 

    // Check if we have previously stored a token. 
    fs.readFile(TOKEN_PATH, function(err, token) { 
    if (err) { 
     getNewToken(oauth2Client, callback); 
    } else { 
     oauth2Client.credentials = JSON.parse(token); 
     callback(oauth2Client); 
    } 
    }); 
} 

: 이미 로그인 구현이 포함되어 있습니다. 그것은 당신의 참조를 위해 JS 샘플을 포함 : 지정된 조회와 일치하는

request = gapi.client.gmail.users.messages.list({ 
      'userId': userId, 
      'pageToken': nextPageToken, 
      'q': query 
     }); 

'Q'만을 반환 메시지를. Gmail 검색 창과 동일한 검색어 형식을 지원합니다 ( ). 예 : "from : [email protected] rfc822msgid : is : 읽지 않음"과 같이 입력하십시오.