@noskiddie

Как отобразить данные на странице из API?

Подскажите как отобразить данные на страницу steps из полученного ответа?
Код

const express = require("express")
const app = express()
const port = 1234
const {google} = require("googleapis")
const request = require("request")
const cors = require("cors")
const urlParse = require("url-parse")
const queryParse = require("query-string")
const bodyParser = require("body-parser")
const axios =   require("axios")
const { response } = require("express")
const path = require("path")
// APP

app.use(cors())
app.use(bodyParser.urlencoded({ extended: true}))
app.use(bodyParser.json())


app.get("/", (req, res) => {
    res.sendFile(path.resolve(__dirname, 'static', 'index.html'))
})

app.get("/getURL", (req, res) => {
    const oauth2Client = new google.auth.OAuth2(
        // client id
        "48888888888888888s0.apps.googleusercontent.com",
        // client secret
        "yv888888888888888888888niZ3",
        // link to redirect
        "http://localhost:1234/steps"
    )

        const scopes = [ "https://www.googleapis.com/auth/fitness.activity.read profile email openid" ]

        const url = oauth2Client.generateAuthUrl({
            access_type: "offline",
            scope: scopes,
            state: JSON.stringify({
                callbackUrl: req.body.callbackUrl,
                userID: req.body.userid
            })
        })

        request(url, (err, response, body) => {
            console.log("error: ", err)
            console.log("statusCode: ", response && response.statusCode)
            res.send({ url })
        })
})

app.get("/steps", async (req, res) => {
    const queryURL = new urlParse(req.url)
    const code = queryParse.parse(queryURL.query).code
    const oauth2Client = new google.auth.OAuth2(
        // client id
        "4718888888888888888888.apps.googleusercontent.com",
        // client secret
        "yvyzz98888888888888niZ3",
        // link to redirect
        "http://localhost:1234/steps"
    )
    
    const tokens = await oauth2Client.getToken(code)
    

    let stepArray = []

    try {
        const result = await axios({
          method: "POST",
          headers: {
            authorization: "Bearer " + tokens.tokens.access_token,
          },
          "Content-Type": "application/json",
          url: `https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate`,
          data: {
            aggregateBy: [
              {
                dataTypeName: "com.google.step_count.delta",
                dataSourceId:
                  "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
              },
            ],
            bucketByTime: { durationMillis: 86400000 },
            startTimeMillis: Date.now() - 30 * 86400000,
            endTimeMillis: Date.now(),
          },
        });

        stepArray = result.data.bucket
    } catch (e) {
        console.log(e)
    }
    try {
        for (const dataSet of stepArray){
            // console.log(dataSet)
            for (const points of dataSet.dataset) {
                // console.log(points)
                for (const steps of points.point) {
                    console.log(steps.value)
                    
                }
            }
        }
    } catch (e) {
        console.log(e)
    }
        
        
        
    res.sendFile(path.resolve(__dirname, 'static', 'steps.html'))

})

app.get



app.listen(port, () => console.log('GFit ready to running on port: 1234'))

  • Вопрос задан
  • 149 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы