Exemplo n.º 1
0
    def create_lite_oauth_url_for_openid(app_id, app_secret, code):
        data = dict()
        data['appid'] = app_id
        data['secret'] = app_secret
        data['js_code'] = code
        data['grant_type'] = 'authorization_code'
        query_str = urlencode(data)

        return "https://api.weixin.qq.com/sns/jscode2session?" + query_str
Exemplo n.º 2
0
    def get_jsapi_ticket(app_id, app_secret):
        """
        获取微信公众号 jsapi_ticket
        :param app_id: 微信公众号应用唯一标识
        :param app_secret: 微信公众号应用密钥(注意保密)
        :return: array 包含 jsapi_ticket 的数组或者错误信息
        """
        data = dict()
        data['appid'] = app_id
        data['secret'] = app_secret
        data['grant_type'] = 'client_credential'
        query_str = urlencode(data)
        access_token_url = \
            'https://api.weixin.qq.com/cgi-bin/token?' + query_str
        client = http_client.new_default_http_client(verify_ssl_certs=verify,
                                                     proxy=proxy,
                                                     ca_bundle=ca_bundle)
        rbody, rcode, headers = client.request('GET', access_token_url, {})
        if hasattr(rbody, 'decode'):
            rbody = rbody.decode('utf-8')
        rbody = util.json.loads(rbody)
        if rcode != 200:
            return rbody

        if 'access_token' not in rbody:
            return None

        data = dict()
        data['access_token'] = rbody['access_token']
        data['type'] = 'jsapi'
        query_str = urlencode(data)
        jsapi_ticket_url = \
            'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' + query_str
        client = http_client.new_default_http_client(verify_ssl_certs=verify,
                                                     proxy=proxy,
                                                     ca_bundle=ca_bundle)
        rbody, rcode, headers = client.request('GET', jsapi_ticket_url, {})
        if hasattr(rbody, 'decode'):
            rbody = rbody.decode('utf-8')
        data = util.json.loads(rbody)
        if rcode == 200:
            return data

        return None
Exemplo n.º 3
0
    def request_raw(self, method, url, params=None, supplied_headers=None):
        """
        Mechanism for issuing an API call
        """

        if self.api_key:
            my_api_key = self.api_key
        else:
            from pingpp import api_key
            my_api_key = api_key

        if my_api_key is None:
            raise error.AuthenticationError(
                'No API key provided. (HINT: set your API key using '
                '"pingpp.api_key = <API-KEY>"). You can generate API keys '
                'from the Ping++ web interface.  '
                'See https://www.pingxx.com/api for details.')

        abs_url = '%s%s' % (self.api_base, url)
        request_uri = url

        if method == 'get' or method == 'delete':
            if params:
                encoded_params = urlencode(list(_api_encode(params or {})))
                abs_url = _build_api_url(abs_url, encoded_params)
                request_uri = _build_api_url(url, encoded_params)
            post_data = None
        elif method == 'post' or method == 'put':
            post_data = util.json.dumps(params).encode("utf-8")
        else:
            raise error.APIConnectionError(
                'Unrecognized HTTP method %r.  This may indicate a bug in the '
                'Ping++ bindings. ' % (method, ))

        headers = self.request_headers(my_api_key, method)

        if supplied_headers is not None:
            for key, value in six.iteritems(supplied_headers):
                headers[key] = value

        request_utc_timestamp = _get_utc_timestamp()
        headers['Pingplusplus-Request-Timestamp'] = request_utc_timestamp
        rsa_data = self.get_rsa_verify_data(post_data, request_uri,
                                            int(request_utc_timestamp))
        util.log_debug('Signing data', data=rsa_data)

        privkey = self.get_private_key()
        if privkey is not None:
            headers['Pingplusplus-Signature'] = self.rsa_sign(
                privkey, rsa_data)

        rbody, rcode, rheaders = self.execute_request_with_retry(
            method, abs_url, headers, post_data)

        return rbody, rcode, rheaders, my_api_key
Exemplo n.º 4
0
    def create_oauth_url_for_openid(app_id, app_secret, code):
        """
        获取openid的URL地址
        :param app_id: 微信公众号应用唯一标识
        :param app_secret: 微信公众号应用密钥(注意保密)
        :param code: 授权code, 通过调用WxpubOAuth.createOauthUrlForCode来获取
        :return: 获取openid的URL地址
        """
        data = dict()
        data['appid'] = app_id
        data['secret'] = app_secret
        data['code'] = code
        data['grant_type'] = 'authorization_code'
        query_str = urlencode(data)

        return "https://api.weixin.qq.com/sns/oauth2/access_token?" + query_str