某些使用Google Apps for Education的学校的Google oAuth2通话失败

问题描述 投票:1回答:2

我们允许使用oauth和google进行身份验证。对于绝大多数客户来说,显示的代码运行良好。但是有一些却失败了。问题与网络无关,因为我可以使用用户学校提供的谷歌电子邮件帐户导致问题,然后切换到个人帐户,而不是出现问题。如果问题确实发生,那么整个学校都会发生这种情况,这让我相信它与谷歌帐户中的设置相关,但似乎没有人知道这个问题。救命!!!

问题在于makeAPICall功能,对于大多数用户,我们可以在此之后访问电子邮件,但在问题学校中,电子邮件未定义。但是没有返回错误。

  <script type="text/javascript" src="https://apis.google.com/js/api.js" "></script>
    <script type="text/javascript">
      var apiKey = 'MY API KEY';
      var discoveryDocs = ["https://people.googleapis.com/$discovery/rest?version=v1"];
      var clientId = 'MY CLIENT ID';
      var scopes = 'profile';

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

        function initClient() {
        gapi.client.init({
            apiKey: apiKey,
            discoveryDocs: discoveryDocs,
            clientId: clientId,
            scope: scopes,
            'immediate': false
        }).then(function () {
          // Listen for sign-in state changes.
          gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
        });
      }

        function updateSigninStatus(isSignedIn) {
            if (isSignedIn) {
                makeApiCall();
            }
      }

      function handleAuthClick() {
          var isSignedIn = gapi.auth2.getAuthInstance().isSignedIn.get();
          if (isSignedIn) {
              makeApiCall();
        }
          else {
              gapi.auth2.getAuthInstance().signIn();
        }
      }

      function makeApiCall() {
        // Load the API and make an API call.  Display the results on the screen.
    // for most users this loads the api and allows me to access the email address.  
        // for certain schools the email addresss is not returned causing lots of problems

        gapi.client.people.people.get({ 
          'resourceName': 'people/me',
          'requestMask.includeField': 'person.emailAddresses'
        }).then(function (resp) {

        //in the case of the email not being returned, the next line errors
          var email = resp.result.emailAddresses[0].value;
          $.ajax({
        //do some application specific stuff using the email address
            }   
          });
        });
      }
    </script>

    <script type="text/javascript" async defer src="https://apis.google.com/js/api.js" 
      onload="this.onload=function(){};handleClientLoad()" 
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
    </script>
google-oauth2 google-people google-apps-for-education
2个回答
1
投票

与我们的一位客户合作后,我们发现Google Apps for Education目录中的信息默认受到保护。学区可以向所有客户开放,也可以由客户提供特定权利。调整完成后,上面显示的代码完美无缺。

谢谢


0
投票

根据the API docs,除了email之外,你还应该要求profile范围。除此之外,您可能想要使用the OAuth2 API UserInfo method(您还需要添加email范围,但它是一个更简单的API)。

© www.soinside.com 2019 - 2024. All rights reserved.