简体   繁体   English

gdrive 上传失败并显示“令牌已过期或撤销”。 如果它使用 crontab 执行但以 root 身份运行 (sudo -s)

[英]gdrive upload fails with "Token has been expired or revoked." if it is executed with crontab but works as root (sudo -s)

I have written a script that uploads/updates an image to my gdrive.我编写了一个脚本,可以将图像上传/更新到我的 gdrive。 It should be executed every 15 min by a crontab entry.它应该由 crontab 条目每 15 分钟执行一次。

I tested the script as root (sudo -s) and it works and has access to my gdrive.我以 root (sudo -s) 身份测试了脚本,它可以工作并且可以访问我的 gdrive。 But it does not work when started by my crontab entry.但是当我的 crontab 条目启动时它不起作用。 I then get the following message as an error.然后我收到以下消息作为错误。

Uploading upload.jpg
Failed to upload file: Patch https://www.googleapis.com/upload/drive/v3/files/1y0000hMVV444qjuxC5xqL3hsmyAuoXya?alt=json&fields=id%2Cname%2Csize&uploadType=multipart: oauth2: cannot fetch token: 400 Bad Request
Response: {
  "error": "invalid_grant",
  "error_description": "Token has been expired or revoked."
}

This is my crontab entry这是我的 crontab 条目

*/15  * * * *   root    /srv/media/webcam/nordbahnstrasse/scripts/upload_image.sh     >> /var/log/upload_image.txt

The gdrive command checks a token stored in token_v2.json . gdrive 命令检查存储在token_v2.json中的令牌。 The default location for that file is within the user's home.该文件的默认位置是在用户家中。

$HOME/.gdrive/token_v2.json

Using sudo -s changes the user, but the variable $HOME still points to the original user.使用 sudo -s 更改用户,但变量 $HOME 仍然指向原始用户。 Running a command with crontab as root checkes for /root/.gdrive/token_v2.json以 crontab 作为 root 运行命令/root/.gdrive/token_v2.json

So the solution is to copy the file token_v2.json to /root/.gdrive/token_v2.json所以解决方法是将文件token_v2.json复制到/root/.gdrive/token_v2.json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM