Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having login required error #1

Open
helloitsm3 opened this issue Jul 9, 2019 · 5 comments
Open

Having login required error #1

helloitsm3 opened this issue Jul 9, 2019 · 5 comments

Comments

@helloitsm3
Copy link

Whenever I try to upload an image, it'll return a "Login Required" error. Can't seem to get around it. I have included the correct email and private key in the authenticate function but it doesn't seem to be able to login and store the image on G drive.

@anhdenday
Copy link

Me too. Did you solved the problem @helloitsm3 ?

@helloitsm3
Copy link
Author

Me too. Did you solved the problem @helloitsm3 ?

Unfortunately, I did not get it to work. I stick to using Multer and Google cloud storage instead.

This is how I initialise my value in my main server

const multer = require("multer");
const { Storage } = require("@google-cloud/storage");

const googleCloudStorage = new Storage({
  projectId: "YOUR_PROJECT_ID",
  keyFilename: PATH_TO_YOUR_SECRET
});

const fileFilter = (req, file, cb) => {
  if (file.mimetype === "image/jpeg" || file.mimetype === "image/png") {
    cb(null, true);
  } else {
    cb(null, false);
  }
};

const upload = multer({
  storage: multer.memoryStorage(),
  limits: { fileSize: 1024 * 1024 * 5 },
  fileFilter: fileFilter
});

const bucket = googleCloudStorage.bucket("YOUR_GCS_BUKET_NAME");

router.post(
  "/uploadimages",
  upload.array("images", 10),
  async (req, res, next) => {
    try {
      const {
       images
      } = req.body;

      const allImages = [];

      if (req.files.length > 0) {
        req.files.map(image => {
          const blob = bucket.file(image.originalname);
          const blobStream = blob.createWriteStream({
            metadata: {
              contentType: image.mimetype
            },
            gzip: true
          });

          blobStream.on("error", err => {
            next(err);
            return;
          });

          blobStream.on("finish", () => {
            const publicUrl = `https://storage.googleapis.com/${bucket.name}/${blob.name}`;

            blob.makePublic().then(() => {
              allImages.push(publicUrl);

              if (req.files.length === allImages.length) {
                  // FUNCTION TO STORE IN DATABASE
                );

                res.send(
                  `Successfully uploaded images`
                );
              }
            });
          });

          blobStream.end(image.buffer);
        });
      }
    } catch (error) {
      console.log(error);
      res.status(500).json();
    }
  }
);

@anhdenday
Copy link

Thanks you for your reply, so you did use Google Cloud Storage instead of Google Drive? I think I have to use GCS too. :)

@helloitsm3
Copy link
Author

Thanks you for your reply, so you did use Google Cloud Storage instead of Google Drive? I think I have to use GCS too. :)

If I understand it correctly, even if you are able to login and upload the images to Google drive, you're unable to display the image on your app. At the end of the day, you still have to use GCS to store your images

@ThisIsAiran
Copy link

Is there no way to solve this "Login Required" error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants