Comments (12)
首先还是一样的操作,拿到 refresh_token
之后,在 Cloudflare Workers →KV 里面的 BUCKET
namespace 中创建一个新的 entry
就叫做 access_token
,然后把你拿到的 refresn_token
粘进去:
之后修改代码,在 ./src/auth/onedrive.js
里面加一个获取 refresh_token
的代码:
// ./src/auth/onedrive.js
// ...
export async function getAccessToken() {
const timestamp = () => {
return Math.floor(Date.now() / 1000)
}
const refresh_token = await BUCKET.get('refresh_token')
// 之后所有的 config.refresh_token 都直接改为 refresh_token
// ...
}
这样应该就可以了,顺便还要把其他原有定义了 REFRESH_TOKEN
的地方删掉(比如 ./src/config/default.js
里面的 refresh_token: REFRESH_TOKEN,
一行)。
from onedrive-cf-index.
第一个问题:preview_id
是用于本地预览的,如果在生成那一步没有传入 --preview
的话是不会生成 preview_id
的,所以不写 preview_id
这个 field 并不影响部署,只是会影响本地预览。
这一步跟 GitHub Actions 也没关系,因为这里两个 ID 都是固定的,直接写入 wrangler.toml
即可。
你可以本地尝试跑一跑带 --preview
和不带 --preview
两个命令,看看输出就知道怎么回事了。
第二个问题: 一样可以用 Cloudflare KV Storage 来存这个 Token。(我稍微写一下)
from onedrive-cf-index.
好的,第一个问题只影响本地预览,不碍事,不填了
第二个问题,谢谢大佬了
from onedrive-cf-index.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from onedrive-cf-index.
首先还是一样的操作,拿到
refresh_token
之后,在 Cloudflare Workers →KV 里面的BUCKET
namespace 中创建一个新的entry
就叫做access_token
,然后把你拿到的refresn_token
粘进去:
之后修改代码,在
./src/auth/onedrive.js
里面加一个获取refresh_token
的代码:// ./src/auth/onedrive.js // ... export async function getAccessToken() { const timestamp = () => { return Math.floor(Date.now() / 1000) } const refresh_token = await BUCKET.get('refresh_token') // 之后所有的 config.refresh_token 都直接改为 refresh_token // ... }
这样应该就可以了,顺便还要把其他原有定义了
REFRESH_TOKEN
的地方删掉(比如./src/config/default.js
里面的refresh_token: REFRESH_TOKEN,
一行)。
key应该是 refresh_token
from onedrive-cf-index.
./src/auth/onedrive.js 72:22
Module parse failed: Identifier 'getAccessToken' has already been declared (72:22)
You may need an appropriate loader to handle this file type.
|
| // ...
export async function getAccessToken() {
| const timestamp = () => {
| return Math.floor(Date.now() / 1000)
@ ./src/index.js 4:0-59 55:28-42 57:42-51
Error: webpack returned an error. You may be able to resolve this issue by running npm install.
按教程操作之后提示这
from onedrive-cf-index.
fa
已经删了
from onedrive-cf-index.
./src/auth/onedrive.js 72:22
Module parse failed: Identifier 'getAccessToken' has already been declared (72:22)
You may need an appropriate loader to handle this file type.
|
| // ...
export async function getAccessToken() {
| const timestamp = () => {
| return Math.floor(Date.now() / 1000)
@ ./src/index.js 4:0-59 55:28-42 57:42-51
Error: webpack returned an error. You may be able to resolve this issue by running npm install.
试过了,还是这样
from onedrive-cf-index.
./src/auth/onedrive.js 72:22
Module parse failed: Identifier 'getAccessToken' has already been declared (72:22)
You may need an appropriate loader to handle this file type.
|
| // ...export async function getAccessToken() {
| const timestamp = () => {
| return Math.floor(Date.now() / 1000)
@ ./src/index.js 4:0-59 55:28-42 57:42-51
Error: webpack returned an error. You may be able to resolve this issue by running npm install.试过了,还是这样
贴一下onedrive.js
from onedrive-cf-index.
import config from '../config/default'
/**
- Get access token for microsoft graph API endpoints. Refresh token if needed.
*/
export async function getAccessToken() {
const timestamp = () => {
return Math.floor(Date.now() / 1000)
}
// Fetch access token
const data = await BUCKET.get('onedrive', 'json')
if (data && data.access_token && timestamp() < data.expire_at) {
console.log('Fetched token from storage.')
return data.access_token
}
// Token expired, refresh access token with Microsoft API. Both international and china-specific API are supported
const oneDriveAuthEndpoint = ${config.apiEndpoint.auth}/token
const resp = await fetch(oneDriveAuthEndpoint, {
method: 'POST',
body: client_id=${config.client_id}&redirect_uri=${config.redirect_uri}&client_secret=${config.client_secret} &refresh_token=${config.refresh_token}&grant_type=refresh_token
,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
if (resp.ok) {
console.info('Successfully refreshed access_token.')
const data = await resp.json()
// Update expiration time on token refresh
data.expire_at = timestamp() + data.expires_in
await BUCKET.put('onedrive', JSON.stringify(data))
console.info('Successfully updated access_token.')
// Finally, return access token
return data.access_token
} else {
// eslint-disable-next-line no-throw-literal
throw getAccessToken error ${JSON.stringify(await resp.text())}
}
}
/**
- Get & store siteID for finding sharepoint resources
- @param {string} accessToken token for accessing graph API
*/
export async function getSiteID(accessToken) {
let data = await BUCKET.get('sharepoint', 'json')
if (!data) {
const resp = await fetch(${config.apiEndpoint.graph}${config.baseResource}?$select=id
, {
headers: {
Authorization:bearer ${accessToken}
}
})
if (resp.ok) {
data = await resp.json()
console.log('Got & stored site-id.')
await BUCKET.put('sharepoint', JSON.stringify({ id: data.id }))
}
}
return data.id
}
// ./src/auth/onedrive.js
// ...
export async function getAccessToken() {
const timestamp = () => {
return Math.floor(Date.now() / 1000)
}
const refresh_token = await BUCKET.get('refresh_token')
// 之后所有的 config.refresh_token 都直接改为 refresh_token
// ...
}
from onedrive-cf-index.
import config from '../config/default'
/**
- Get access token for microsoft graph API endpoints. Refresh token if needed.
*/
export async function getAccessToken() {
const timestamp = () => {
return Math.floor(Date.now() / 1000)
}// Fetch access token
const data = await BUCKET.get('onedrive', 'json')
if (data && data.access_token && timestamp() < data.expire_at) {
console.log('Fetched token from storage.')
return data.access_token
}// Token expired, refresh access token with Microsoft API. Both international and china-specific API are supported
const oneDriveAuthEndpoint =${config.apiEndpoint.auth}/token
const resp = await fetch(oneDriveAuthEndpoint, {
method: 'POST',
body:client_id=${config.client_id}&redirect_uri=${config.redirect_uri}&client_secret=${config.client_secret} &refresh_token=${config.refresh_token}&grant_type=refresh_token
,
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
if (resp.ok) {
console.info('Successfully refreshed access_token.')
const data = await resp.json()// Update expiration time on token refresh data.expire_at = timestamp() + data.expires_in await BUCKET.put('onedrive', JSON.stringify(data)) console.info('Successfully updated access_token.') // Finally, return access token return data.access_token
} else {
// eslint-disable-next-line no-throw-literal
throwgetAccessToken error ${JSON.stringify(await resp.text())}
}
}/**
- Get & store siteID for finding sharepoint resources
- @param {string} accessToken token for accessing graph API
*/
export async function getSiteID(accessToken) {
let data = await BUCKET.get('sharepoint', 'json')
if (!data) {
const resp = await fetch(${config.apiEndpoint.graph}${config.baseResource}?$select=id
, {
headers: {
Authorization:bearer ${accessToken}
}
})
if (resp.ok) {
data = await resp.json()
console.log('Got & stored site-id.')
await BUCKET.put('sharepoint', JSON.stringify({ id: data.id }))
}
}
return data.id
}// ./src/auth/onedrive.js
// ...
export async function getAccessToken() {
const timestamp = () => {
return Math.floor(Date.now() / 1000)
}const refresh_token = await BUCKET.get('refresh_token')
// 之后所有的 config.refresh_token 都直接改为 refresh_token
// ...
}
才发现。。你这加的什么东西。。。
你用我库里的吧。。。
onedrive.js
from onedrive-cf-index.
大佬,用你刚刚给的库部署好了。一样的操作,不晓得啥情况。
from onedrive-cf-index.
Related Issues (20)
- wrangler login 执行时卡在 Waiting for API token... HOT 2
- 希望能添加个视图预览 HOT 1
- cloudflare works 使用路由开文件提示重定向次数过多 HOT 1
- 关于上传文件问题
- Custom domain does not works HOT 1
- 从使用 wrangler 添加 Cloudflare Workers 环境变量开始报错(小白求助) HOT 10
- [bug?] OAuth Refresh token过期 HOT 2
- Preview cache (seems to) break Markdown preview
- 使用Insomnia获取refresh_token被告知code已过期
- 从6月份开始,Cf用户报错Worker threw exception,我这边报错Script Threw Exception,重新再来还是如此。 HOT 1
- Error 1101 Worker threw exception HOT 2
- 能否添加一个图床功能 HOT 2
- 叨扰大佬一下 我捣鼓了一晚上 一直获取不了refresh_token HOT 1
- It's not necessary to register new applications HOT 1
- 找不到 zone_id HOT 4
- Publish时出现以下错误 HOT 4
- 输入wrangler login 命令后报错 HOT 1
- Custom Domain Worker threw exception 1101 [自訂域名拋出 1101 錯誤] HOT 1
- 在进行到修改 src/config/default.js后该怎么办呢,是像原heymind一样将js填入worker的部署页面还是怎么操作呢(本人纯新手,不懂wrangler知识) HOT 1
- Markdown渲染base64编码图片时出现错误
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from onedrive-cf-index.