如何使用Google帐户自动登录到我的网页?

时间:2018-06-06 08:24:06

标签: javascript html google-api

在我的网页中,我显示在" Google日历"上创建的所有活动,通过选择用户的Google帐户来加载数据。这工作正常。但我想自动传递登录信息而不点击任何按钮,即,只要在浏览器中打开网页,日历中的数据就会通过定义的"谷歌帐户"出现在特定字段中。 我正在使用" python-3.7.0b5-embed-amd64 - zip文件"作为我的服务器。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <!--<link rel="icon" type="image/png" href="imges/favicon.ico">-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>Soft Pass Counter</title>
    <meta content='width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0' name='viewport' />
    <meta name="viewport" content="width=device-width" />
     <!-- <meta http-equiv="refresh" content="05"> -->
    <!-- Bootstrap core CSS     -->
    <link href="css/bootstrap.min.css" rel="stylesheet" />
    <!--     Fonts and icons     -->
    <link href="css/font-awesome.min.css" rel="stylesheet">
    <link href='http://fonts.googleapis.com/css?family=Roboto:400,700,300' rel='stylesheet' type='text/css'>
</head>
<style>
body {
    overflow: hidden;
}
@font-face {
    font-family: Mallory Bold;
    font-weight: Bold;
    src: url("path/Mallory Bold.otf") format("opentype");
}
@font-face {
    font-family: Mallory Light;
    font-weight: Light;
    src: url("path/Mallory Light.otf") format("opentype");
}
@font-face {
    font-family: Sakamoto Medium;
    src: url("path/Sakamoto Medium.otf") format("opentype");
}
.room_status1
{
    width: 50px;
    height: 26vw;   
}
.room_name
{
    color: #000000;
    font-size: 3vw;
    font-family: Mallory;   
}
.meeting_name
{
    color: #000000;
    font-size: 2vw;
    font-family: Mallory;   
}
.organizer_name
{
    color: #000000;
    font-size: 1.5vw;
    font-family: Mallory;   
}
.next_meeting_name
{
    color: #000000;
    font-size: 2vw;
    font-family: Mallory;
    margin-top: 75px;
}
img {
    width: 100%;
    height: auto;
}
#main_frame{
    width: 100%;
    height: 100%;
    background-image: url("images/BG.png");
    position: fixed;
    //padding: 10px;
    background-position: center;
    overflow-y: auto;
    overflow-x: hidden;
}
.sign_in_button{
    opacity: 0.4;
    padding: 6px 15px;
    text-align: center;
    font-size: 10px;
    float: left;
    margin: 10px 0 0 10px;      
}
.sign_out_button{
    opacity: 0.4;
    padding: 6px 15px;
    text-align: center;
    font-size: 10px;
    float: right;
    margin: 10px;
}
.rm_name
{
    height: 0px;
}
hr
{
    margin-top: 58px;
    margin-bottom: -13px;
    border-top: 2px solid #E8E8E8;
}
.table
{
border: 2px solid white;
}
</style>
<body>
        <div id="main_frame" class="container" >
            <button onclick="myFunction()" id="authorize-button" class="sign_in_button"><b>Confirm</b></button>
            <button onclick="myFunction()" id="signout-button" class="sign_out_button"><b>Sign Out</b></button>
            <div class="row" align="center">
                <p style="margin:10em;" align="center"><img style="max-height:100%; margin-top:-80px;" src="images/SB_G_logo.png" /></p>
            </div>
            <p id="current_time" align="center" style="font-size:8vw; font-family: Sakamoto; font-weight: Bold; color:#BBBCBE; margin-bottom:33px; margin-top:-112px;"></p>

            <div class="row">
                <div id="room_1" class="col-lg-6 col-md-6 col-sm-6 col-xs-12 table-responsive" >
                    <table class="table">
                    <tbody>
                        <tr class="rm_name">
                            <td rowspan="5" id="room_1_status" class="room_status1"></td>
                            <td id="organizer" class="room_name">BOND STREET</td>
                        </tr>
                        <tr class="rm_name">
                            <td  id="content" class="meeting_name"></td>
                        </tr>
                        <tr class="rm_name">
                            <td  id="content2" class="organizer_name"></td>
                        </tr>
                        <tr>
                            <td><hr style="color: #E8E8E8;"/></td>
                        </tr>
                        <tr class="rm_name">
                            <td  id="content1" class="next_meeting_name"></td>
                        </tr>
                    </tbody>
                  </table>
                </div>
            </div>
            <div>
                <p id="current_day" style="font-family: Sakamoto; font-weight: bold; text-align: center; margin-top: 30px; font-variant: normal; font-size: 8vw; color: #BBBCBE"></p>
                <p id="current_date" style="font-family: Sakamoto; font-weight: bold;text-align: center; font-variant: normal; font-size:8vw; color: #BBBCBE"></p>
            </div>
        </div>
<script type="text/javascript" src="js/scriptTime.js"></script>
<script type="text/javascript" src="js/scriptEventList1.js"></script>
<script type="text/javascript">window.onload = date_time('current_time');</script>
<script async defer src="https://apis.google.com/js/api.js"
      onload="this.onload=function(){};handleClientLoad()"
      onreadystatechange="if (this.readyState === 'complete') this.onload()">
</script>
<script>
var d = new Date(),
    months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
    days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];

document.getElementById("current_day").innerHTML = days[d.getDay()];
document.getElementById("current_date").innerHTML = d.getDate() + " " + months[d.getMonth()]+" "+d.getFullYear();
</script>
<script>
function myFunction() {
    window.location.reload();
}
</script>
</body>
</html>

scriptEventList1.js

     var CLIENT_ID = '834563803336-rcvoi3va2qq8bcftfslp0r5ekk33cgep.apps.googleusercontent.com';
      var API_KEY = 'AIzaSyC6zb1gcE1lqSqpb3hZQ14inlyxspPAMxA';
      var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"];
      var SCOPES = "https://www.googleapis.com/auth/calendar.readonly";
      var authorizeButton = document.getElementById('authorize-button');
      var signoutButton = document.getElementById('signout-button');

        var Room1Name;
        var Room2Name;

        var RoomTime1=[];       
        var RoomEndTime1=[];

        var itemRoom1=0;
        var flagRoom1=0;

        var eventOrganizerName1 = [];
        var eventNameRoom1 = [];

        //var Room_1_Name_Ideal = "Conference Room";

         var id1 = "AVAILABLE";
         var id = "NEXT MEETING - ";

        var room1=0;
        var room2=0;
        var room3=0;
        var room4=0;
        var room5=0;
        var room6=0;

      function handleClientLoad() {
        gapi.load('client:auth2', initClient);
      }

      function initClient() {
        gapi.client.init({
          apiKey: API_KEY,
          clientId: CLIENT_ID,
          discoveryDocs: DISCOVERY_DOCS,
          scope: SCOPES
        }).then(function () {
          // Listen for sign-in state changes.
          gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
          // Handle the initial sign-in state.
          updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
          authorizeButton.onclick = handleAuthClick;
          signoutButton.onclick = handleSignoutClick;
        });
      }

      function updateSigninStatus(isSignedIn) {
        if (isSignedIn) {
            authorizeButton.style.display = 'none';
            signoutButton.style.display = 'block';
            listUpcomingEvents();
        } 
        else {
            authorizeButton.style.display = 'block';
            signoutButton.style.display = 'none';
        }
      }
      function handleAuthClick(event) {
        gapi.auth2.getAuthInstance().signIn();
      }
      function handleSignoutClick(event) {
        gapi.auth2.getAuthInstance().signOut();
      }

       //-------------------For Room1------------------------

      function appendPre(message) {
        var pre = document.getElementById('content');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }


        function appendPre1(message) {
        var pre = document.getElementById('content1');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }

       function appendPre2(message) {
        var pre = document.getElementById('content2');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
      }

      function timeToUpdate(){
            var d = new Date().toString();
            var dd = d.getMinutes();
            if(dd.includes("00")){
                listUpcomingEventsRoom1();
                listUpcomingEventsRoom2();
                appendPre('in time');
                return true;
            }
            else
                return false;
      }

        function listUpcomingEvents() {
        gapi.client.calendar.events.list({
          'calendarId': 'primary',
          'timeMin': (new Date()).toISOString(),
          'showDeleted': false,
          'singleEvents': true,
          'maxResults': 10,
          'orderBy': 'startTime'
        }).then(function(response) {
          var events = response.result.items;
          var d = new Date().toString();
          var dateCur = d.substring(0,15);
          var k=0;
          var m=0;
          var n=0;
          var o=0;
          var p=0;
          var q=0;

       if (events.length > 0) {
            for (i = 0; i < events.length; i++) {
                var event = events[i];
                var dateCurr = event.date;
                var when = event.start.dateTime;
                var whenEnd = event.end.dateTime;
                if (!when) {
                    when = event.start.date;
                }

           //Calling the function to verify todays date.
           var dateTod = verifyDate(when);
           var loc = event.location;

            if(dateTod){
                if(loc.includes("MindstekBuildingOne")){
                    if(loc.includes('BOND STREET' )){
                        if(k<2){
                        var startTime = when.substring(11);
                        var startTime1 = startTime.substring(0,5);
                        RoomTime1.push(startTime1);

                        var endTime = whenEnd.substring(11);
                        var endTime1 = endTime.substring(0,5);
                        RoomEndTime1.push(endTime1);
                        isAvailableRoom1(startTime1, endTime1);
                        eventNameRoom1.push(event.summary);
                        eventOrganizerName1.push(event.organizer.displayName);
                        k++;                        
                        }
                    }
                    else if(loc.includes('COVENT GARDEN')){
                        if(m<2){
                        var startTime2 = when.substring(11);
                        var startTime3 = startTime2.substring(0,5);
                        RoomTime2.push(startTime3);

                        var endTime2 = whenEnd.substring(11);
                        var endTime3 = endTime2.substring(0,5);
                        RoomEndTime2.push(endTime3);
                        isAvailableRoom2(startTime3, endTime3);
                        eventNameRoom2.push(event.summary);
                        eventOrganizerName2.push(event.organizer.displayName);
                        m++;
                        }
                    }
                }
            }
        }
        //------------------------Room1------------------------------------//
        if(flagRoom1==1){       //Room1 Meeting going on
            if(eventNameRoom1[0]==undefined){
                appendPre(RoomTime1[0] +' to ' + RoomEndTime1[0]);
            }
            else{
                appendPre(eventNameRoom1[0]+' - '+RoomTime1[0] +' to ' + RoomEndTime1[0]);
            }
            appendPre2(eventOrganizerName1[0]);
            if(RoomTime1[1]== undefined){

                appendPre1('NO MEETING');
                document.getElementById('content1');
            }
            else{
                appendPre1('Next Meeting-'+RoomTime1[1] +' to ' + RoomEndTime1[1]);
            }
            document.getElementById("room_1_status").style.backgroundColor = '#FF0101';
        }
        else{                   //Room1 Available
            appendPre(id1);
            if(RoomTime1[0]== undefined){
                appendPre1('NO MEETING');
            }
            else{
                appendPre1(id+RoomTime1[0] +' to ' + RoomEndTime1[0]);
            }
            document.getElementById("room_1_status").style.backgroundColor = '#01FF37';
        }   

    }
    else 
        {
          appendPre(id1);
          appendPre1('NO MEETING');
          document.getElementById("room_1_status").style.backgroundColor = '#01FF37';
        }
    });
}    

    //to verify today's date 
    function verifyDate(when){

         var d = new Date();
         var dateCur = d.getDate();
        var res = when.substring(8);
        var dateToday = res.substring(0,2);
        if(dateToday==dateCur)
            return true;
        else
            return false; 
     }

     //Room1 Availability
    function isAvailableRoom1(start, end){

        var startTimeObject = new Date();
        var endTimeObject = new Date(startTimeObject);

        var d = new Date();
         var minCur = d.getMinutes();
         var hourCur = d.getHours();
         var meetinHour;
         var meetinMin;
          var meetinEndHour;
         var meetinEndMin;

         meetinHour = start.substring(0,2);
         var meetinHour1 = parseInt(meetinHour, 10);

         meetinMin = start.substring(0,5);
         var meetinMin1 = meetinMin.substring(3);

         var meetinMin2 = parseInt(meetinMin1, 10);

          meetinEndHour = end.substring(0,2);
         var meetinEndHour1 = parseInt(meetinEndHour, 10);

         meetinEndMin = end.substring(0,5);
         var meetinEndMin1 = meetinEndMin.substring(3);

         var meetinEndMin2 = parseInt(meetinEndMin1, 10);

            if(itemRoom1<1){
                itemRoom1 = 1;
            if(meetinHour1>=hourCur||hourCur>=meetinEndHour1)
            {
                if(meetinHour1==hourCur){
                    if(meetinMin2<=minCur){
                        flagRoom1=1;
                    }
                }
                else if(meetinEndHour1==hourCur){
                    if(meetinEndMin2>=minCur){
                        flagRoom1=1;
                    }
                }
            }
        }

    }

0 个答案:

没有答案