Hi there I created a custom API in google cloud but when I try to access the API it only gives me the google thing for signing into google cloud console
script for access API:
const axios = require('axios');
const { google } = require('googleapis');
// Load the service account key file
const serviceAccountKey = require('./serviceAccountKey.json');
// Configure Axios to use Google authentication
const auth = new google.auth.GoogleAuth({
credentials: serviceAccountKey,
scopes: ['https://www.googleapis.com/auth/cloud-platform']
});
axios.defaults.headers.common['Authorization'] = `Bearer ${auth.getAccessToken().then(response => response.token)}`;
// Define the URL of your API endpoint
const apiUrl = 'https://3002-cs-105746203933-default.cs-us-east1-vpcf.cloudshell.dev/api/userids/Dan';
// Make a GET request to the API endpoint
axios.get(apiUrl)
.then(response => {
// Handle successful response
console.log('Response:', response);
})
.catch(error => {
// Handle error
console.error('Error:', error.message);
});
the actual api code:
const express = require("express");
const mongoose = require("mongoose");
const PORT = process.env.PORT || 3002;
// Connect to MongoDB database
mongoose.connect("mongodb+srv://<username>:<password>@cluster0.aviwp.mongodb.net/CarboCredit");
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
db.once("open", function () {
console.log("Connected to MongoDB");
});
// Define a schema for the userids "table" (sub-collection)
const userIdsSchema = new mongoose.Schema({
// Define the fields relevant to the userids "table"
// For example:
id: Number,
// Other fields...
});
// Define a model based on the schema
const UserIds = mongoose.model("CarboCredit", userIdsSchema, "userids");
const app = express();
app.get("/api/userids/:userId", async (req, res) => {
try {
// Get the userId from request parameters
const { userId } = req.params;
// Find a document in UserIds sub-collection with the given userId
const user = await UserIds.findOne({username: userId});
// If no document found with the given userId
if (!user) {
return res.status(404).json({ error: "User not found" });
}
// Document found, return it as JSON response
res.json(user);
} catch (err) {
console.error("Error fetching user:", err);
res.status(500).json({ error: "Internal server error" });
}
});
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
the code gives me the correct output when I access it inside google cloud vm but I want to access it in another google cloud VM it gives me a link to log into google cloud VM and when I tried to through post mate it was the same issue and I when I try to login it gives me a 400 error I was wondering if this could be bypassed or dissabled somehow