Get access token from pocket

This commit is contained in:
Hongbo Wu 2023-02-24 12:22:07 +08:00
parent da1fdb85f9
commit ae2f695df8
4 changed files with 23 additions and 7 deletions

View file

@ -67,12 +67,14 @@ export const setIntegrationResolver = authorized<
} else {
// Create
const integrationService = getIntegrationService(input.name)
// validate token
if (!(await integrationService.validateToken(input.token))) {
// authorize and get access token
const token = await integrationService.accessToken(input.token)
if (!token) {
return {
errorCodes: [SetIntegrationErrorCode.InvalidToken],
}
}
integrationToSave.token = token
}
// save integration

View file

@ -4,8 +4,8 @@ import { Page } from '../../elastic/types'
export abstract class IntegrationService {
abstract name: string
validateToken = async (token: string): Promise<boolean> => {
return Promise.resolve(true)
accessToken = async (token: string): Promise<string | null> => {
return Promise.resolve('')
}
export = async (
integration: Integration,

View file

@ -21,6 +21,20 @@ export class PocketIntegration extends IntegrationService {
name = 'POCKET'
POCKET_API_URL = 'https://getpocket.com/v3'
accessToken = async (token: string): Promise<string | null> => {
const url = `${this.POCKET_API_URL}/oauth/authorize`
try {
const response = await axios.post<{ access_token: string }>(url, {
consumer_key: env.pocket.consumerKey,
code: token,
})
return response.data.access_token
} catch (error) {
console.log('error validating pocket token', error)
return null
}
}
retrievePocketData = async (
accessToken: string,
since: number

View file

@ -38,7 +38,7 @@ export const READWISE_API_URL = 'https://readwise.io/api/v2'
export class ReadwiseIntegration extends IntegrationService {
name = 'READWISE'
validateToken = async (token: string): Promise<boolean> => {
accessToken = async (token: string): Promise<string | null> => {
const authUrl = `${env.readwise.apiUrl || READWISE_API_URL}/auth`
try {
const response = await axios.get(authUrl, {
@ -46,10 +46,10 @@ export class ReadwiseIntegration extends IntegrationService {
Authorization: `Token ${token}`,
},
})
return response.status === 204
return response.status === 204 ? token : null
} catch (error) {
console.log('error validating readwise token', error)
return false
return null
}
}
export = async (