Giter Site home page Giter Site logo

Comments (12)

spencerwooo avatar spencerwooo commented on July 22, 2024 1

首先还是一样的操作,拿到 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, 一行)。

from onedrive-cf-index.

spencerwooo avatar spencerwooo commented on July 22, 2024

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

image

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

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

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

from onedrive-cf-index.

weiyunkj666 avatar weiyunkj666 commented on July 22, 2024

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

第二个问题,谢谢大佬了

from onedrive-cf-index.

stale avatar stale commented on July 22, 2024

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.

SakuraPuare avatar SakuraPuare commented on July 22, 2024

首先还是一样的操作,拿到 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

from onedrive-cf-index.

0-RTT avatar 0-RTT commented on July 22, 2024

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

0-RTT avatar 0-RTT commented on July 22, 2024

fa

已经删了

from onedrive-cf-index.

0-RTT avatar 0-RTT commented on July 22, 2024

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

SakuraPuare avatar SakuraPuare commented on July 22, 2024

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

0-RTT avatar 0-RTT commented on July 22, 2024

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.

SakuraPuare avatar SakuraPuare commented on July 22, 2024

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

from onedrive-cf-index.

0-RTT avatar 0-RTT commented on July 22, 2024

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

from onedrive-cf-index.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.