来自gmail的电子邮件,但响应是gmail.readonly

时间:2014-10-17 11:41:16

标签: gmail-api

我正在尝试使用https://www.googleapis.com/auth/gmail.readonly/?access_token='fdrt654vfdgfe6545从gmail收到电子邮件但是作为回应我收到gmail.readonly但是System.HttpResponse[Status=OK, StatusCode=200]没问题。可以指导我有什么我错过了。

这个我如何请求获得auth但在响应中我收到了访问令牌

<script type="text/javascript">
  (function() {
   var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
   po.src = 'https://apis.google.com/js/client:plusone.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
 })();
</script>



  <script type="text/javascript">
      (function() {
           var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
           po.src = 'https://apis.google.com/js/client:plusone.js';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
     })();

     var AuthStates = {google: null};

     function signinCallback(authResult) {
          if (authResult['status']['signed_in']) {                  
            // Update the app to reflect a signed in user
            // Hide the sign-in button now that the user is authorized, for example:
            document.getElementById('signinButton').setAttribute('style', 'display: none');
            //alert(authResult['code']);
            console.log('code state: ' + authResult['code']);
            //console.log('authResult : ' + authResult[]);
            AuthStates.google = authResult;
            console.log('authResult 1 : ' + authResult['status']['method']);
            console.log('auth Result : ' + authResult['id_token']);
            //{!access_token} = authResult['access_token'];
            //{!code} = authResult['code'];
            connection(authResult['access_token'], authResult['code']);
          } else {
            // Update the app to reflect a signed out user
            // Possible error values:
            //   "user_signed_out" - User is signed-out
            //   "access_denied" - User denied access to your app
            //   "immediate_failed" - Could not automatically log in the user
            console.log('Sign-in state: ' + authResult['error']);
          }
        }

  </script>


  <apex:outputPanel >
      <span id="signinButton">
          <span
            class="g-signin"
            data-callback="signinCallback"
            data-clientid="clientid"
            data-cookiepolicy="single_host_origin"                
            data-scope="https://www.googleapis.com/auth/gmail.readonly"
            data-response_type="code"
            data-redirect_uri="http://test-on.ap1.visual.force.com/apex/Gmail_inbox">
          </span>
    </span>
    <apex:form >

因此,当我从我的请求中获取访问令牌时,我可以直接获取与logedin用户相关的所有信息。当我收到有关用户的所有信息时,我正在尝试获取与他相关的所有电子邮件。我做得对,或者我错了任何地方。我是一个非常新的api和web服务试图学习。请帮我救出来。

1 个答案:

答案 0 :(得分:1)

您实际上正在对该网址进行HTTP GET调用(https://www.googleapis.com/auth/gmail.readonly/?access_token= ...)?是不是只有auth范围标识符

获得有效的Oauth2令牌后,您可以在Auth标头中设置,然后您可以向API发出HTTP请求。例如,要列出消息,请参阅: https://developers.google.com/gmail/api/v1/reference/users/messages/list

具有要访问的网址(GET https://www.googleapis.com/gmail/v1/users/me/messages),然后,当您拥有消息ID后,您可以单独获取消息:https://developers.google.com/gmail/api/v1/reference/users/messages/get(例如GET https://www.googleapis.com/gmail/v1/users/me/messages/)。

通常,Google API都有很好的客户端库。不确定这是否适合您,但请参阅,例如: https://developers.google.com/gmail/api/downloads