[英]How can I get user's location without asking for the permission or if user permitted once so need not to ask ever again?
[英]How can access google calendar of user and edit it without asking for user permisssion again and again
在我的網站上,我要求訪問谷歌日歷。 我可以編輯用戶日歷,但我不想一次又一次地請求用戶許可,因此一旦用戶授權並授予對谷歌日歷的訪問權限,我可以隨時編輯它,直到用戶撤銷訪問權限。 我應該在前端還是后端實現它,如何實現? 我檢查了幾個答案,他們提到我們可以使用服務帳戶,但是不清楚如何編輯或閱讀單個用戶的日歷事件以及如何在用戶撤銷訪問權限后將其刪除。 此問題已刪除,因為缺少代碼,因此在下面添加代碼。
我試過這個,所以一旦用戶登錄,我就會得到訪問令牌並且我正在使用它
window.gapi.load("client:auth2", () => {
window.gapi.client.setApiKey("api_key");
window.gapi.client.load("https://content.googleapis.com/discovery/v1/apis/calendar/v3/rest")
.then(() => {
window.gapi.auth.setToken({ access_token: access_token })
window.gapi.client.calendar.events.insert({
"calendarId": "id",
'resource': event
}).then((res) => {
console.log("calendar data res "+JSON.stringify(res))
}).catch(err => console.log("error getting calendar data "+JSON.stringify(err)))
}).catch(err => console.error("Error loading GAPI client for API", err) )
})
但是一旦訪問令牌過期,我如何才能獲得新的訪問令牌(我不想一次又一次地向用戶顯示登錄彈出窗口。我想知道如何在客戶端使用刷新令牌來做到這一點)。
如果不向公眾公開您的密鑰,您將無法在客戶端獲取刷新令牌。
您可以創建一個接受 oAuth 代碼的端點並返回令牌,保存刷新令牌以備后用。 您設置了一個玉米作業來檢查過期令牌並刷新它們。
每次用戶訪問您的應用程序時,您都會從服務器獲取一個新的令牌並繼續正常工作。
根據谷歌指南。 你做POST
到https://oauth2.googleapis.com/token
。 假設您的服務器端堆棧位於 Node.js 中,您可以使用 HTTP 客戶端(如Axios)執行以下操作:
const Axios = require('axios');
const Qs = require('querystring');
const GOOGLE_CLIENT_ID = 'abc';
const GOOGLE_CLIENT_SECRET = '123';
let refreshToken = getFromDataBase(); // should be stored in database
Axios.post('https://oauth2.googleapis.com/token', Qs.stringify({
client_id: GOOGLE_CLIENT_ID,
client_secret: GOOGLE_CLIENT_SECRET,
refresh_token: refreshToken,
grant_type: 'refresh_token'
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}
})
.then(({ data }) => console.log(data.access_token)) // new token that expires in ~1 hour
.catch(console.log)
首先,您是否 (a) 想要在用戶未登錄時更新日歷,例如響應外部事件? ...或者...您 (b) 是否只想從瀏覽器 session 中更新日歷?
如果是 (a),那么您需要要求用戶進行離線訪問,這將為您提供一個刷新令牌,您可以將其安全地存儲在服務器上並在需要時使用。 (忘記所有關於服務帳戶的事情)。
如果(b),那么您需要以下信息:-
prompt=none
。 這將為您提供一個新的訪問令牌,而用戶不會看到任何 UX。localStorage
或postMessage
與您的應用程序共享。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.