2021年10月5日 星期二

use Google Calendar API in web page via javascript

I am going to write this article in English, so my sister can read it.

First of all, u need to believe it is very easy, I just tried, here is step by step tutorial.

1. go to google console via your web browser.

https://console.developers.google.com/apis

you're gonna have to login to your google account first.

2. create a new project for this.

add a new project


click create


new project created

3. Enable "Google Calendar API" in the "Library" menu

click "Library" menu int the picture above.
search API "calendar" and press "Enter"

click "Google Calendar API" found.

click "Enable"

4. Create your app and set it up


click "CREATE CREDENTIALS"

choose "User Data" and click "Next"

name your app and choose your self as the app supporter


upload an icon for the app, type your EMail and save it

5. decide the information your users need to provide to you

so you can know who they are.

just click it.


choose what you want to know from your app users, for example, their email for further contact or event notify.

click "Update" after finish choosing

then save it 

6. create your app's client id and put it in your web page javascript

So the Google API server will know that it is your app, not others'.

name your client id that you will put in your web page javascript 

set the domain that you are going to use your app (the Google calendar API), so other hackers' domain will not be able to use it



click "Create" to create  your app's client id

copy it and past it in your notepad for latter use, as you can see, for testing purpose, you need to add users for testing this app. by default, no one can use it ! I will explain how to do it latter.

client id created, you can copy it again any time you want.

7. create your API key for google server to check your quota and access

for this project, you may create multiple API keys, and put them in different web page. So google will log their behavior separately.

create API key


API key created, you may want to restrict it's use from web page only, so other hackers will not be able to try it from the windows app they wrote.

make some restriction rules.

8. add app testing users

as we mentioned before, you need to add testing users, or no one can use it.

9. write the javascript in your web page

for Google Calendar API Reference:

https://developers.google.com/calendar/api/v3/reference

for javascript example, you can visit the following webapage.

https://developers.google.com/calendar/api/quickstart/js

please make sure you set your CLIENT_ID and API_KEY in the example code:

here is the result of the example code:





however, this example shows you only the way to login.
here is another example, please make sure you set your CLIENT_ID and API_KEY in the following code:


<!DOCTYPE html>
<html>
<head>
<title>Google Calendar API</title>
<meta charset="UTF-8" />
<meta name="description" content="test Google Calendar API">
<script src="https://apis.google.com/js/api.js"></script>   
</head>
<body>
<p>Google Calendar API</p>
<button id="loginButton">log in .</button>
<button onclick="add2EventAtCalendar()">add party events at 2021-10-10 and 2021-10-13</button>
<P>
<a href="https://calendar.google.com/calendar/r/month/2021/10/1?tab=rc" target="_blank">show Google 10/2021 Calender</a>

<script type="text/javascript">
var CLIENT_ID = '1031293676717-e1osoq4b89qm3njucjar6bo0jsg5qam3.apps.googleusercontent.com';
    var API_KEY = 'AIzaSyCF7YkpJ4lzepNYoKgSP8WBCsGMs0bqR-g';

    var authParams = {
  'response_type' : 'token',
  'client_id' : CLIENT_ID, 
  'immediate' : false,
'scope' : ['https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.events'] 
};

    //init
gapi.load("client:auth2", function() {
gapi.auth2.init({client_id: CLIENT_ID});
});

//login btn set onclick event
document.getElementById('loginButton').onclick = function(){
  gapi.auth.authorize(authParams, loginAndSetToken)
};

//login and set token
function loginAndSetToken(returnToken){
  if (returnToken && returnToken['access_token']) {
gapi.auth.setToken(returnToken);
console.log(returnToken)
setAPIandLoadClient();
  } else {
    alert("Auth failed");
  } }

//set API key and load client
function setAPIandLoadClient() {
gapi.client.setApiKey( API_KEY );
return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/calendar/v3/rest").then(
function() { console.log("GAPI client loaded for API"); },
function(err) { console.error("Error loading GAPI client for API", err); }
);
}

//add 2 calendar events
function add2EventAtCalendar() {
alert("c1");
const events = [ {
'summary': 'Join the party No.1 at 10/20',
'location': 'My sweet home',
'description': 'https://www.cyccea.org.tw/',
'start': {
'date': '2021-10-20',
'timeZone': 'America/New_York'
},
'end': {
'date': '2021-10-21',
'timeZone': 'America/New_York'
}
},
{
'summary': 'Join the party No.2 at 10/26',
'location': 'My sweet garden',
'description': 'https://www.cyccea.org.tw/',
'start': {
'date': '2021-10-26',
'timeZone': 'America/New_York'
},
'end': {
'date': '2021-10-27',
'timeZone': 'America/New_York'
}
}];
//multi events
const batch = gapi.client.newBatch();

events.map((r, j) => {
batch.add(gapi.client.calendar.events.insert({
'calendarId': 'primary',
'resource': events[j]
}))
})
batch.then(function(){
console.log('2 events add successfully !')
});
}
</script>
</body>
</html>

and here is the result :






2021年10月1日 星期五

Andronix 手機變 Linux 桌機,學生開學買電腦!白買啦!學生省錢大作戰!


 大家好,我是快樂奧卡斯,今天我要介紹的是 Andronix,就是 Android 加 Linux,讓你的 Android 手機變桌機!

出門旅遊「手機辦公」真麻煩?學生買了手機,沒錢買電腦 ?用 Andronix 將 Android 手機變桌機,辦公、寫作業不再卡卡卡卡!

立刻馬上在手機或平板上,享用「桌機」版的 office、文書、簡報、和繪圖軟體,來大大大大的提昇辦公效率呦!商務出差,不再需要背著厚重的筆電,到了旅社飯店,手機直接連上電視,就可以開始辦公啦!

所有安裝流程,請看以下影片:

很多人都不知道,Windows 可以用 cigwin 來執行 Linux 程式,Linux 可以用 Wine 來執行 Windows 程式,而在 Android 上,可以用 Termux 來執行 Linux 程式,至於 「Andronix」,則是使用 「Termux」 ,來建立一個 Linux 桌面。

它們都不是虛擬機,所以不會佔用大量的「記憶體」和 「CPU」,直接橋接程式庫,讓執行速度飛快快快!

要安裝 「安卓你客司Andronix」 ,要先安裝,可以用來安裝「其他軟件」的「軟件」F-Droid,「F-著衣的」功能和 google 的「play 商店」 差不多,但它只針對「開放原始碼」的軟件,並會對軟件中「非開放原始碼」的部份發出警告,這對於「手機安全性要求高」的用戶,非常非常重要呦!

安裝4個軟體的第1個「F-著衣的 F-Droid」

「F-著衣的」的 「下載」和「安裝」,請看上面影片中的展示。

安裝4個軟體的第2個「特馬克思 Termux」

接下來,我們在「F-著衣的」搜尋並安裝 「特馬克思Termux」 軟件,請注意,安裝「特馬克思」需要 Android 7 以上的版本,「Google play 商店」也可以安裝舊版的「特馬克思」,但它和我們等等要安裝的「安卓-你客司」不相容喔!

安裝4個軟體的第3個「遠端桌面連線客戶端 VNC Viewer」

切換到手機桌面,啟動 Google 「Play 商店」,搜尋 v-n-c 空格 v-i-e-w-e-r ,按下「安裝」,這是「遠端桌面連結」,我們將透過它,來操作「桌機桌面」!

安裝4個軟體的第4個「安卓-你客司 Andronix」

接著再搜尋 a-n-d-r-o-n-i-x 並按下安裝,按下開啟,可以看到一堆 「安卓-你客司」 打包好的「閹割版」免費 Linux,常見的 ubuntu 、 Kali 、Debian  、 arch 、Manjaro 、fedora、void、 alpine 都有,畫面下面提供 Moded OS 則是要錢的,要錢的便利性會比較高,舉例來說,如果你手機有自己安裝記憶卡的話,它會自動幫你掛載到檔案總管裡,先試一個免費的 Ubuntu 20.04 CFCE 桌面版吧!它的桌面雖然比簡易還簡易,但可以把更多的「記憶體」和 「cpu」,全部都用在你的工作上呦。

Ubuntu安裝

按下安裝後,畫面上提示,「安裝指令」已經複製(至此,Andronix APP已經沒用途了,除非你想要反安裝,或是改裝其他的 Linux),視窗往下捲,按下「開啟 特馬克思Termux」,手指長按「手機螢幕」,再選擇「貼上」,按 Enter,開始進入 Ubuntu 安裝,詳細畫面步驟請參看上面影片介紹。

Ubuntu 安裝完畢,預設 vnc 桌面,只能從「本手機」連入 5901 port,輸入並設定 vnc 登入密碼,它同時會是「系統管理者」 root 的密碼,啟動 VNC 伺服器,閹割版 ubuntu 啟動完成,預設用系統管理者 root 登入,往後「系統異動」打指令,也不須加 「sudo」 來切換成系統管理者了

使用 VNC Viewer 連進手機裡的 Ubuntu 桌機桌面

此時,你是看不到這個桌面的,要看到這個桌面,須使用 VNC Viewer,啟動手機上剛剛安裝的 VNC 遠端桌面連結,新增一個遠端桌面,輸入本機 127.0.0.1:5901,連上去,他會提示連線沒加密,因為是本機,所以沒關係,按 OK,輸入剛剛設定的 VNC 密碼,第一次使用 vnc ,它想教你「如何操作」,答應它吧,除了常用的:

「兩指縮放螢幕」和「單指移動滑鼠」
「滑鼠右鍵」是「兩指一起按」
「滑鼠中鍵」是「三指一起按」
「滑鼠滾輪」是「兩指拖拉」
「圈選文字」或「移動視窗」是「單指點兩下拖拉」
「螢幕頂端」向下滑,可以顯示「工具列」
「工具列」中的「鍵盤鈕」可喚出「螢幕鍵盤」
「工具列」中的「滑鼠鈕」可喚出「螢幕滑鼠」

關閉教學,就看到「桌機」桌面囉,說了老半天,建議你還是跟我的片頭一樣,用 OTG 外接「滑鼠、鍵盤、和繪圖板」,使用起來才方便!

首次使用手機上的 Ubuntu 桌面,建議先縮小「螢幕解析度」,讓「螢幕字體」變大,點進「檔案總管」,可以看到一個路徑「SDCard」,點進去其實是手機內存路徑,這樣可以方便你和 Andorid 手機互換檔案。

如何重新啟動這個閹割板的 ubuntu

先說如何重新啟動這個閹割板的 ubuntu,手機切換回 「特馬克思」視窗,輸入 Exit 可以將 ubuntu 瞬間關機,輸入 ls 可以查看 「安卓-你客司」 的檔案配置,輸入 bash 空格 start 減 ubuntu20.sh 可以啟動 ubuntu,瞬間啟動完成,因為它只是橋接啦!

安裝中文4步驟-1.安裝這個閹割板 ubuntu 的螢幕中文字型

這個閹割版的 ubuntu,沒有中文字型,所以上網中文都是亂碼,安裝中文很容易,只要安裝所有 zh 和 chinese 開頭的「語言包」和「字型」,我會將指令寫在影片下方供你複製使用。

影片11分17秒 安裝中文支援 以及 中文字型 指令

sudo apt-get install language-pack-zh*

sudo apt-get install chinese*

sudo apt-get install fonts-arphic-ukai fonts-arphic-uming fonts-ipafont-mincho fonts-ipafont-gothic fonts-unfonts-core

再次打開瀏覽器,中文就正常顯示了。

安裝中文4步驟-2.設定中文輸入法 gcin 安裝來源

影片11分45秒,安裝中文輸入法 gcin 須在 /etc/apt/sources.list 檔案的開頭,加上兩行內容如下:

deb http://www.calno.com unstable main
deb-src http://www.calno.com unstable main

安裝中文4步驟-3.安裝 gcin 所需元件、gcin輸入法、和輸入法切換 的指令

影片11分45秒,接著使用終端機,輸入指令安裝 cgin 的元件、輸入法、和輸入法切換:

sudo apt-get update
sudo apt-get install libappindicator3-1 gcin im-switch

安裝中文4步驟-4.修改 xfce 桌面輸入法設定檔,改成採用 gcin 輸入法

影片12分26秒,修改 xfce 桌面輸入法設定檔/etc/xdg/xfce4/xinitrc,檔案的開頭,加上以下內容,改成採用 cgin 輸入法:

export LC_CTYPE=zh_TW.UTF-8
export GTK_IM_MODULE=gcin
export XMODIFIERS="@im=gcin"
gcin &

以上主要是要設定,語言使用中文 zh_TW,輸入法使用 gcin,最後一行代表重新啟動 cgin 服務。

存檔後,用前面講過的方法,重新啟動 ubuntu,設定才會生效喔!

安裝 LibreOffice

打開 ubuntu 預先安裝好的 libreoffice,如果找不到,也可以自己打指令來安裝:

apt install libreoffice 

按下 ctrl - 空白鍵,切換中文輸入法,中文輸入成功!

安裝比美 PHOTOSHOP 的繪圖軟體 GIMP

再來安裝比美 PHOTOSHOP 的繪圖軟體 GIMP,安裝指令是:

apt install gimp

安裝影片剪接軟體 kdenlive

安裝指令是:

apt install kdenlive

安裝比美 Illustrator的向量繪圖軟體 InkScape

安裝指令是:

apt install inkscape

安裝寫程式的軟體 Net-beans

爸爸也想安裝看看它寫程式的軟體 Net-beans,安裝指令是:

apt install netbeans

測評:

是不是所有 linux 上的軟體都能安裝呢,答案絕對是否定的,舉例來說,微軟 Linux 版的的 vscode 和 mssql 資料庫,只支援 AMD64 cpu,不支援手機的 ARM cpu,所以無法安裝,其他像是「中文桌面」、「Ubuntu 軟體中心」、顯卡導向的「vlc」、「shot-cut」、「i386 cpu 的 wine32」、「我和弟弟玩街機電動的MAME」,還有「爸爸寫程式用的 eclipse」,在這個「閹割版」的  ubuntu 上,目前都還無法安裝,或是安裝後無法使用呦,好了,今天的節目到此為止,記得按讚、分享和按下小鈴鐺喔! bye bye !

補充說明:從其他電腦的 VNC viewer 連進手機裡的 Ubuntu 桌機桌面

如果要從其他電腦的 VNC viewer 連進手機裡的 Ubuntu 桌機桌面呢?首先你要讓這台電腦,和手機在同一個網段裡,最簡單的方法,就是事先設定手機 wifi 熱點,將網路分享給這台電腦,接著,你要在 Ubuntu 中安裝網路管理工具,指令如下:

apt install net-tools

再執行以下指令查看 Ubuntu 的 IP 位置(你也可以直接到Android手機裡的設定查看):

ifconfig

然後執行以下指令停止 VNC Server:

vncserver-stop

再執行以下指令,啟動 VNC Server 使用「非本機」連結:

vncserver-start -localhost no

如此,這台電腦就可以透過 Ubuntu 的 IP 連上來了!

本篇文章來源:

https://arielfunox.wixsite.com/cool/post/andronix-%E6%89%8B%E6%A9%9F%E8%AE%8A%E6%A1%8C%E6%A9%9F%EF%BC%8C%E5%AD%B8%E7%94%9F%E9%96%8B%E5%AD%B8%E8%B2%B7%E9%9B%BB%E8%85%A6%EF%BC%81%E7%99%BD%E8%B2%B7%E5%95%A6%EF%BC%81