<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[mo-lab - Medium]]></title>
        <description><![CDATA[For Good and Fun - Medium]]></description>
        <link>https://medium.com/mo-lab?source=rss----aef7c836e846---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>mo-lab - Medium</title>
            <link>https://medium.com/mo-lab?source=rss----aef7c836e846---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 13:31:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/mo-lab" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Google Refresh Token]]></title>
            <link>https://medium.com/mo-lab/google-refresh-token-c54e44090ed5?source=rss----aef7c836e846---4</link>
            <guid isPermaLink="false">https://medium.com/p/c54e44090ed5</guid>
            <category><![CDATA[google-api]]></category>
            <category><![CDATA[refresh-token]]></category>
            <dc:creator><![CDATA[Clouday]]></dc:creator>
            <pubDate>Tue, 26 Mar 2019 08:36:30 GMT</pubDate>
            <atom:updated>2019-03-26T08:42:19.519Z</atom:updated>
            <content:encoded><![CDATA[<p>由於公司專案需求，需要把公司的服務串接到google行事曆，以Server-to-Server的方式。<br>跟一般的串接方式有些不同，這次需求需要取得永久性token，以方便日後與server的串接。</p><p>一些相關心得記錄如下：</p><p>要取得永久性的token，可以分為以下三個部分</p><pre><strong>一，於Google Developers Console建立帳號＆專案</strong></pre><pre>在Google Developers Console建立帳號，以及建立相對應的API專案</pre><pre><strong>二，帳號授權使用Google行事曆API，並取得永久 Refresh Token</strong></pre><pre>一般流程下的Token，在一個小時後就會失效，而由於專案需求的服務是Server-to-Server的緣故，需要與Google取得不會失效方式</pre><pre><strong>三，利用 Refresh Token，取得 Access Token</strong></pre><p><strong>串接流程大抵如下</strong></p><blockquote>- app授權 &gt;</blockquote><blockquote>- 取得Long-lived Refresh Token &gt;</blockquote><blockquote>- 將Long-lived Refresh Token存下 &gt;</blockquote><blockquote>- 未來利用 Long-lived Refresh Token 取得 Short-lived Access Token &gt;</blockquote><blockquote>- 利用 Short-lived Access Token 串接 Google api</blockquote><p><strong>心得：</strong></p><p>一，Google的Long-lived Token機制與facebook不同，fb可以直接拿到Long-lived Token。<br>但google只能拿到Long-lived Refresh Token，再經由Long-lived Refresh Token換取Short-lived Access Token，多了一層的防護。</p><p>二，據官方的說法，Long-lived Refresh Token會在以下情況下失效</p><ul><li>使用者取消授權該app</li><li>六個月以上未使用該Long-lived Refresh Token</li><li>使用者更改google密碼，且該app有相關於Gmail的權限</li><li>對單一app取得超過50個以上的Long-lived Refresh Token，發出第51個Long-lived Refresh Token後，第1個Long-lived Refresh Token就會取消</li></ul><p><strong>一，Google Developers Console建立帳號＆專案</strong></p><p>在 Google Developers Console 建立帳號網路上有非常多的教學，而google介面在每經過一段時間後，UI也會有所微幅變動，就不贅述建立的步驟。但有一點是需要注意的！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/247/0*42y0n_ZkcxAe_BLy" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/625/0*Fy3B5rxg0zKoaXIF" /></figure><p>建立專案取得API金鑰，或是OAuth 2.0 用戶端 ID，建議一定要設定應用程式限制，避免讓外部的人有機會使用自己的API金鑰。</p><p><strong>二，帳號授權使用Google行事曆API，並取得永久 Refresh Token</strong></p><p>在 Google Developers Console 建立並取得OAuth 2.0 用戶端 ID後，要串接Google行事曆api，首先要取得一個想要串接的帳號授權。</p><p>取得授權網址參數如下</p><blockquote><strong>https://accounts.google.com/o/oauth2/auth</strong></blockquote><blockquote><strong>scope=https://www.googleapis.com/auth/calendar<br></strong> (此次專案所需授權的Google服務</blockquote><blockquote><strong>redirect_uri= 授權後指向之網址 </strong><br>(需與OAuth 2.0授權 &gt; 導向URI 一樣</blockquote><blockquote><strong>response_type=code <br></strong>(code / token，使用code的方式，取得refresh token</blockquote><blockquote><strong>client_id= OAuth 2.0 用戶端 ID</strong></blockquote><blockquote><strong>access_type=offline <br></strong>(online / offline，為了取得 refresh token，需要取得offline權限</blockquote><blockquote><strong>prompt=consent<br></strong> ( 是否每次授權時，都跳出授權視窗，在此範例建議開啟</blockquote><blockquote>*網址皆需urlEncode</blockquote><p><a href="https://developers.google.com/identity/protocols/OAuth2WebServer">https://developers.google.com/identity/protocols/OAuth2WebServer</a><br>授權後經由回傳的code參數，取得 Long-lived Refresh Token。</p><blockquote><strong>https://www.googleapis.com/oauth2/v4/token</strong></blockquote><blockquote><strong>grant_type=authorization_code<br></strong>(填寫authorization_code，取得Short-lived Access Token＆Long-lived Refresh Token</blockquote><blockquote><strong>code= 從授權導回頁取得的code參數</strong></blockquote><blockquote><strong>client_id= app ID</strong></blockquote><blockquote><strong>client_secret= app Secret</strong></blockquote><blockquote><strong>redirect_uri=授權後導回的頁面網址</strong></blockquote><pre>Return JSON：</pre><pre>[access_token] =&gt; ya29.GlvTBlOm_____</pre><pre>[expires_in] =&gt; 3600</pre><pre>[refresh_token] =&gt; 1/hxrd_______</pre><pre>[scope] =&gt; <a href="https://www.googleapis.com/auth/calendar">https://www.googleapis.com/auth/calendar</a></pre><pre>[token_type] =&gt; Bearer</pre><p><a href="https://developers.google.com/identity/protocols/OAuth2WebServer#creatingclient">https://developers.google.com/identity/protocols/OAuth2WebServer#creatingclient</a></p><p><strong>三，利用 Refresh Token，取得 Access Token</strong></p><blockquote><strong>https://www.googleapis.com/oauth2/v4/token</strong></blockquote><blockquote><strong>client_id= app ID</strong></blockquote><blockquote><strong>client_secret= app Secret</strong></blockquote><blockquote><strong>refresh_token= 存下來的Refresh Token</strong></blockquote><blockquote><strong>grant_type=refresh_token <br></strong>(填寫refresh_token，取得Short-lived Access Token</blockquote><pre>Return JSON：</pre><pre>[access_token] =&gt; ya29.Gl3TBmvWw________</pre><pre>[expires_in] =&gt; 3600</pre><pre>[scope] =&gt; <a href="https://www.googleapis.com/auth/calendar">https://www.googleapis.com/auth/calendar</a></pre><pre>[token_type] =&gt; Bearer</pre><p><a href="https://developers.google.com/identity/protocols/OAuth2WebServer#offline">https://developers.google.com/identity/protocols/OAuth2WebServer#offline</a></p><p><strong>Other Reference：</strong></p><p><a href="https://developers.google.com/identity/protocols/OAuth2#expiration">https://developers.google.com/identity/protocols/OAuth2#expiration</a></p><p><a href="http://usefulangle.com/post/29/google-calendar-api-create-event-php">http://usefulangle.com/post/29/google-calendar-api-create-event-php</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c54e44090ed5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mo-lab/google-refresh-token-c54e44090ed5">Google Refresh Token</a> was originally published in <a href="https://medium.com/mo-lab">mo-lab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>