0

我已经使用以下代码成功创建了访问令牌。


    client_secrets = Google::APIClient::ClientSecrets.load("#{Rails.root}/config/client_secrets.json")
      auth_client = client_secrets.to_authorization
      auth_client.update!(
        :scope => 'https://mail.google.com/ https://www.googleapis.com/auth/gmail.modify https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/pubsub',
        :redirect_uri => 'http://localhost:3000/google_code',
        :additional_parameters => {
          "access_type" => "offline",         # offline access
          "include_granted_scopes" => "true"  # incremental auth
        }
      )

      auth_client.code = auth_code

要回复 Gmail 邮件,我正在使用此代码。

gmail = Google::Apis::GmailV1::GmailService.new
        
          gmail.authorization = authorizer

          message              = Mail.new
          message.date         = Time.now
          message.subject      = "Re: Test02" 
          message.from         = "email@gmail.com"
          message.to           = "email@gmail.com"
          message.message_id   = "\u003cCAFVVPDvD-zK6Akj8djj+uam35ZxFTf2EW4AefW1Ri7OV7AxFVA@mail.gmail.com\u003e"

          message.part content_type: 'multipart/alternative' do |part|
              part.html_part = Mail::Part.new(body: "Using service Account.", content_type: 'text/html; charset=UTF-8')
          end

          msg = message.encoded
            
          message_object = Google::Apis::GmailV1::Message.new(raw:message.to_s, thread_id: "1764cc4c5efa6855", content_type: 'message/rfc822')
          

          gmail.send_user_message('israr.kinectro@gmail.com', message_object)

第一次(通过谷歌身份验证屏幕登录)我能够成功发送消息。

但是,如果我再次尝试使用相同的访问令牌或生成的凭据文件,则会出现未授权错误。

注意:访问令牌是有效的。

我想使用生成的有效访问令牌发送消息。

4

0 回答 0