Python问题:AttributeError:'dict'对象没有属性'upper'

时间:2011-05-02 01:19:56

标签: python google-app-engine twitter

尝试发布推文时,我从AppEngine收到此错误。我认为我的语法不对,但我现在感到很茫然。

对于我的生活,我似乎无法使用以下命令将我的状态参数发送到post函数中的** extra_params:

args = {} <br/>
args.update({'status': 'THIS IS MY TWEET'})<br/>
info2 = client.post('/statuses/update',args)<br/>

确切错误是:

"File "/base/data/home/apps/kymbatweet/1.350119792863170339/main.py", line 311, in get_signed_body
    __meth.upper(), __url, '&'.join(
AttributeError: 'dict' object has no attribute 'upper'"

有什么想法吗? (代码如下)

    def post(self, api_method, http_method='POST', expected_status=(200,), **extra_params):

        if not (api_method.startswith('http://') or api_method.startswith('https://')):
            api_method = '%s%s%s' % (
                self.service_info['default_api_prefix'], api_method,
                self.service_info['default_api_suffix']
                )

        if self.token is None:
            #self.token = OAuthAccessToken.get_by_key_name(self.get_cookie())
            self.token = "000"



        fetch = urlfetch(url=api_method, payload=self.get_signed_body(
            api_method, "000", http_method, **extra_params
            ), method=http_method)

        if fetch.status_code not in expected_status:
            raise ValueError(
                "Error calling... Got return status: %i [%r]" %
                (fetch.status_code, fetch.content)
                )

        return decode_json(fetch.content)



    def get_signed_body(self, __url, __token=None, __meth='GET',**extra_params):

        service_info = self.service_info

        kwargs = {
            'oauth_consumer_key': service_info['consumer_key'],
            'oauth_signature_method': 'HMAC-SHA1',
            'oauth_version': '1.0',
            'oauth_timestamp': int(time()),
            'oauth_nonce': getrandbits(64),
            }

        kwargs.update(**extra_params)
        #This works if hardcoded
        #kwargs.update({'status': 'AE'})

        if self.service_key is None:
            self.service_key = get_service_key(self.service)

        if __token is not None:
            kwargs['oauth_token'] = "000"
            key = self.service_key + encode("6iD2LYnUfEOYl0zOCj5IKawbok3pjs4yixay5bdM")
        else:
            key = self.service_key
        #Code BOMBS HERE
        message = '&'.join(map(encode, [
            __meth.upper(), __url, '&'.join(
                '%s=%s' % (encode(k), encode(kwargs[k])) for k in sorted(kwargs)
                )
            ]))

        kwargs['oauth_signature'] = hmac(
            key, message, sha1
            ).digest().encode('base64')[:-1]

        return urlencode(kwargs)



class MainHandler(RequestHandler):
    """Demo Twitter App."""

    def get(self):

        client = OAuthClient('twitter', self)

        client.token = "000"


        args = {}
        args.update({'status': 'THIS IS MY TWEET'})

        info2 = client.post('/statuses/update',args)

2 个答案:

答案 0 :(得分:6)

您没有将其作为extra_params传递,而是将其作为http_method传递。

info2 = client.post('/statuses/update', **args)

答案 1 :(得分:1)

如果你想从字典中传递额外的参数(关键字参数),你需要像这样调用它:

client.post('/path', **args)