Skip to content
This repository has been archived by the owner on Jan 1, 2022. It is now read-only.

refresh_token 大于 1KB 导致需要使用 Cloudflare Worker KV 存储来解决的方法 #58

Closed
weiyunkj666 opened this issue Nov 25, 2020 · 12 comments
Labels
question Common questions wontfix This will not be worked on

Comments

@weiyunkj666
Copy link

weiyunkj666 commented Nov 25, 2020

wrangler.toml中的preview_id = ""应该填什么?我使用的是GitHub Actions

使用Firebase 数据库时在Cloudflare [Workers填写在大于1KB的变量时按照这个教程可以解决https://github.com//issues/16

但现在使用Cloudflare KV不知道怎么操作了。。。希望大佬指点迷津

@spencerwooo
Copy link
Owner

spencerwooo commented Nov 25, 2020

第一个问题:preview_id 是用于本地预览的,如果在生成那一步没有传入 --preview 的话是不会生成 preview_id 的,所以不写 preview_id 这个 field 并不影响部署,只是会影响本地预览。

image

这一步跟 GitHub Actions 也没关系,因为这里两个 ID 都是固定的,直接写入 wrangler.toml 即可。

你可以本地尝试跑一跑带 --preview 和不带 --preview 两个命令,看看输出就知道怎么回事了。

第二个问题: 一样可以用 Cloudflare KV Storage 来存这个 Token。(我稍微写一下)

@spencerwooo spencerwooo added the question Common questions label Nov 25, 2020
@weiyunkj666
Copy link
Author

weiyunkj666 commented Nov 25, 2020

好的,第一个问题只影响本地预览,不碍事,不填了

第二个问题,谢谢大佬了

@spencerwooo
Copy link
Owner

spencerwooo commented Nov 25, 2020

首先还是一样的操作,拿到 refresh_token 之后,在 Cloudflare Workers →KV 里面的 BUCKET namespace 中创建一个新的 entry 就叫做 access_token,然后把你拿到的 refresn_token 粘进去:

image

之后修改代码,在 ./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, 一行)。

@stale
Copy link

stale bot commented Dec 6, 2020

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.

@stale stale bot added the wontfix This will not be worked on label Dec 6, 2020
@stale stale bot closed this as completed Dec 7, 2020
@spencerwooo spencerwooo pinned this issue Jan 23, 2021
@spencerwooo spencerwooo changed the title 使用Cloudflare KV几个问题 refresh_token 大于 1KB 导致需要使用 Cloudflare Worker KV 存储来解决的方法 Jan 23, 2021
@SakuraPuare
Copy link

首先还是一样的操作,拿到 refresh_token 之后,在 Cloudflare Workers →KV 里面的 BUCKET namespace 中创建一个新的 entry 就叫做 access_token,然后把你拿到的 refresn_token 粘进去:

image

之后修改代码,在 ./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

@0-RTT
Copy link

0-RTT commented Feb 25, 2021

./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.

按教程操作之后提示这

@0-RTT
Copy link

0-RTT commented Feb 25, 2021

fa

已经删了

@0-RTT
Copy link

0-RTT commented Feb 25, 2021

./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.

试过了,还是这样

@SakuraPuare
Copy link

./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

@0-RTT
Copy link

0-RTT commented Feb 25, 2021

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
// ...
}

@SakuraPuare
Copy link

SakuraPuare commented Feb 25, 2021

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
// ...
}

才发现。。你这加的什么东西。。。
你用我库里的吧。。。
onedrive.js

@0-RTT
Copy link

0-RTT commented Feb 25, 2021

大佬,用你刚刚给的库部署好了。一样的操作,不晓得啥情况。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Common questions wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants