title: How can I fix the TypeError in the given code?
body: <p>I am getting &quot;TypeError: Unicode-objects must be encoded before hashing&quot; at line <code>self['oauth_body_hash'] = base64.b64encode(sha1(self.body).digest())</code></p>
  <pre><code>def sign_request(self, signature_method, consumer, token):
   #Set the signature parameter to the result of sign.
   if not self.is_form_encoded:
     self['oauth_body_hash'] = base64.b64encode(sha1(self.body).digest())
  
   if 'oauth_consumer_key' not in self:
     self['oauth_consumer_key] = consumer.key
   
   if token and 'oauth_token' not in self:
     self['oauth_token'] = token.key
  
   self['oauth_signature_method'] = signature_method.name
   self['oauth_signature'] = signature_method.sign(self, consumer, token)
  </code></pre>
  <p>These things I have tried:</p>
  <pre><code>1)self['oauth_body_hash'] = base64.b64encode(hashlib.sha1(str.encode(self.body)).digest())
  2)self['oauth_body_hash'] = base64.encodestring(hashlib.sha256(self.body.encode('UTF-8')).digest().strip())
  </code></pre>