laravel-gmail icon indicating copy to clipboard operation
laravel-gmail copied to clipboard

Unable to save attachments: 401

Open robfrancken opened this issue 4 years ago • 3 comments

It seems I can't save message attachments to disk. I am using GOOGLE_ALLOW_MULTIPLE_CREDENTIALS=true and I'm able to retrieve messages for a given account, but not the attachments. Not sure if I'm doing something wrong.

I have a token stored in storage/app/gmail/tokens/gmail-json-accountA.json.

This works:

$messages = LaravelGmail::setUserId('accountA')
    ->message()
    ->unread()
    ->take(3)
    ->preload()
    ->all();

foreach ($messages as $message) {
    dd($message->getSubject()); // Test with attachment
}

But when I try to save the attachments:

foreach ($messages as $message) {
    foreach ($message->getAttachments() as $attachment) {
         $attachment->saveAttachmentTo('attachments', $attachment->getFileName(), 'local');
    }
}

I get:

  {                                                                                                                                                                                                               
    "error": {                                                                                                                                                                                                    
      "code": 401,                                                                                                                                                                                                
      "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-i  
  n/web/devconsole-project.",                                                                                                                                                                                     
      "errors": [                                                                                                                                                                                                 
        {                                                                                                                                                                                                         
          "message": "Login Required.",                                                                                                                                                                           
          "domain": "global",                                                                                                                                                                                     
          "reason": "required",                                                                                                                                                                                   
          "location": "Authorization",                                                                                                                                                                            
          "locationType": "header"                                                                                                                                                                                
        }                                                                                                                                                                                                         
      ],                                                                                                                                                                                                          
      "status": "UNAUTHENTICATED"                                                                                                                                                                                 
    }                                                                                                                                                                                                             
  } 

Do I need to set the access token manually or how to I resolve this error?

robfrancken avatar Aug 27 '20 01:08 robfrancken

Same here. If you dd the attachments, you'll notice that "token" is null. When MULTIPLE_ACCOUNTS was not set, it was working. My suspect is that it does not read correctly the json file

Edit. just had confirmation.

I created a symlink from the token "gmail-json-accountA.json" to gmail-json.json, and it started working again, token is present. We need to find where it looks for the wrong token file.

sfinetti avatar Jan 19 '21 21:01 sfinetti

I found a possible workaround, is in the Trait "Configurable.php". it seems a problem with user auth, it is hardcoded as auth()->user()->id, but if you, like use, use another field for user id, it goes wrong. I changed to auth()->user()->email and it started working again.

sfinetti avatar Jan 19 '21 22:01 sfinetti

I'm having the same issue. I there a fix or workaround yet?

ChrisSantiago82 avatar Dec 31 '21 14:12 ChrisSantiago82