duanniu4106 2018-01-17 17:26
浏览 42
已采纳

PHP项目与谷歌API和JavaScript:继承混乱

So I have inherited a few projects at work that user php. I have no experience using php so of course have been struggling to fix a problem we found. At this point I am simply just trying to get the google id of who ever happens to be logged in when they visit the site. Weird thing is that some of the time my code works great and i get the google id, but it only works for some of the accounts we have tried, others just return null. The code below I am just trying to get who is signed in right, but I am so ignorant of how this all works that I can't tell what's wrong.

<!DOCTYPE html>
<html class="no-js" lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>Career Opportunities</title>


  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <script src="https://apis.google.com/js/client:platform.js"></script>
  <script type="text/javascript">
    function startUp(onSuccess, onFailure) {
      gapi.client.init({
        apiKey: 'API KEY',
        clientId: 'CLIENTID.apps.googleusercontent.com',
        scope: 'profile',
      }).then(function() {
        var x = gapi.auth2.getAuthInstance();
        console.log("gapi.auth2.getAuthInstance() : " + x);
            console.log("userId is : " + x.currentUser.get().getId());

        gapi.client.load('people', 'v1', function() {
          var request = gapi.client.people.people.get({
            'resourceName': 'people/me',
            'requestMask.includeField': 'person.phone_numbers,person.organizations,person.email_addresses,person.names'
          });
          console.log('request: ' + request);
          request.execute(function(resp) {
            console.log(resp);

          });
        });
      });

    }
    gapi.load('client', startUp);
  </script>


  <script src="js/validation.js" async defer></script>



</head>

<body>



</body>

</html>

This is the error i get when it doesn't like the user for whatever reason. EDIT: This error is in the console. This page does nothing but show me who is logged in via the console.log.

code:401

data:undefined

error:{code: 401, data: undefined, message: "The request does not have valid authentication credentials."}

message:"The request does not have valid authentication credentials."

proto:Object

I have tried so many different things that I assume this code will be a conglomeration of a bunch of different approaches, sorry lol.

EDIT- Added more console.logs - edited above code to reflect this

bobtest.php:101 gapi.auth2.getAuthInstance() : [object Object]
bobtest.php:102 userId is : null
bobtest.php:108 request: [object Object]
cb=gapi.loaded_0:148 GET https://content-people.googleapis.com/v1/people/me?requestMask.includeField=person.phone_numbers&key=401 ()
zu @ cb=gapi.loaded_0:148
n @ cb=gapi.loaded_0:148
Cu @ cb=gapi.loaded_0:149
(anonymous) @ cb=gapi.loaded_0:149
g @ cb=gapi.loaded_0:123
c @ cb=gapi.loaded_0:119
postMessage (async)
(anonymous) @ cb=gapi.loaded_0:122
setTimeout (async)
call @ cb=gapi.loaded_0:122
call @ cb=gapi.loaded_0:134
l @ cb=gapi.loaded_0:195
(anonymous) @ cb=gapi.loaded_0:194
g @ cb=gapi.loaded_0:126
c @ cb=gapi.loaded_0:122
postMessage (async)
(anonymous) @ cb=gapi.loaded_0:119
setTimeout (async)
call @ cb=gapi.loaded_0:119
call @ cb=gapi.loaded_0:131
_.Xj.Rl @ cb=gapi.loaded_0:149
_.Xj.kc @ cb=gapi.loaded_0:149
window.startup @ proxy.html?usegapi=1&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.tZDXb_qbHrc.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Frs%:8
(anonymous) @ googleapis.proxy.js?onload=startup:10
ba @ googleapis.proxy.js?onload=startup:3
b @ googleapis.proxy.js?onload=startup:10
Y.r.(anonymous function) @ googleapis.proxy.js?onload=startup:10
Y.x.(anonymous function) @ googleapis.proxy.js?onload=startup:11
(anonymous) @ cb=gapi.loaded_0:1
bobtest.php:110 {code: 401, data: undefined, message: "The request does not have valid authentication credentials.", error: {…}}

EDIT 3 Added more console.logs

----STARTING CLIENT.INIT
bobtest.php:101 ----CLIENT.INIT SUCCESSFUL
bobtest.php:103 gapi.auth2.getAuthInstance() : [object Object]
bobtest.php:104 userId is : null
bobtest.php:110 ----CLIENT.LOAD SUCCESSFUL
bobtest.php:111 request: [object Object]
cb=gapi.loaded_0:148 GET https://content-people.googleapis.com/v1/people/me?requestMask.includeField=person.phone_numbers%2Cperson.organizations%2Cperson.email_addresses%2Cperson.names&key=AIzaSyATLxJElwrDoYaBJtcWCOOP4JI7CUKlXUk 401 ()
zu @ cb=gapi.loaded_0:148
n @ cb=gapi.loaded_0:148
Cu @ cb=gapi.loaded_0:149
(anonymous) @ cb=gapi.loaded_0:149
g @ cb=gapi.loaded_0:123
c @ cb=gapi.loaded_0:119
postMessage (async)
(anonymous) @ cb=gapi.loaded_0:122
setTimeout (async)
call @ cb=gapi.loaded_0:122
call @ cb=gapi.loaded_0:134
l @ cb=gapi.loaded_0:195
(anonymous) @ cb=gapi.loaded_0:194
g @ cb=gapi.loaded_0:126
c @ cb=gapi.loaded_0:122
postMessage (async)
(anonymous) @ cb=gapi.loaded_0:119
setTimeout (async)
call @ cb=gapi.loaded_0:119
call @ cb=gapi.loaded_0:131
_.Xj.Rl @ cb=gapi.loaded_0:149
_.Xj.kc @ cb=gapi.loaded_0:149
window.startup @ proxy.html?usegapi=1&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.tZDXb_qbHrc.O%2Fm%3D__features__%2Fam%3DAQ%2Frt%3Dj%2Fd%3D1%2Frs%3DAGLTcCOgQjGru3ruZo70XwAerRmnfOfOeA:8
(anonymous) @ googleapis.proxy.js?onload=startup:10
ba @ googleapis.proxy.js?onload=startup:3
b @ googleapis.proxy.js?onload=startup:10
Y.r.(anonymous function) @ googleapis.proxy.js?onload=startup:10
Y.x.(anonymous function) @ googleapis.proxy.js?onload=startup:11
(anonymous) @ cb=gapi.loaded_0:1
bobtest.php:113 ----REQUEST.EXECUTE CALLBACK
bobtest.php:114 {code: 401, data: undefined, message: "The request does not have valid authentication credentials.", error: {…}}

EDIT 4 Added the successful console log

----STARTING CLIENT.INIT

bobtest.php:101 ----CLIENT.INIT SUCCESSFUL
bobtest.php:103 gapi.auth2.getAuthInstance() : [object Object]
bobtest.php:104 userId is : 9999(actually shows id, just removed just in case)
bobtest.php:110 ----CLIENT.LOAD SUCCESSFUL
bobtest.php:111 request: [object Object]
bobtest.php:113 ----REQUEST.EXECUTE CALLBACK
bobtest.php:114 {resourceName: "people/9999999", etag: "%999999=", names: Array(1), emailAddresses: Array(1), result: {…}}
</div>
  • 写回答

2条回答 默认 最新

  • douxie7339 2018-01-18 14:55
    关注

    I didn't really find this answer, my coworker did, but i figured I should post what we ended up getting to work.

    gapi.auth.authorize({
                'client_id': myClientId,
                'scope': 'email',
                'immediate': true
            }, function(authResult) {
                if (authResult['status']['signed_in']) {
                    // Update the app to reflect a signed in user
                    gapi.client.load('plus','v1', function(){
                        var request = gapi.client.plus.people.get({'userId': 'me'});
                        request.execute(function(resp) {
    
                            var userId = '<?php echo $_SESSION["userID"]; ?>';
    
                            if(userId == resp.id) {
                                onSuccess();
                            }
                            else {
                                onFailure();
                            }
                        });
                    });
                }
                else {
                    onFailure();
                }   
            });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?