def linkedin(self, request, credentials, network):
     oauthRequest = oauth.makeOauthRequestObject('https://api.linkedin.com/v1/people/~/person-activities',
                                                 network.getCredentials(), token=oauth.OAuthToken.from_string(credentials.token),
                                                 method='POST')
     headers = {'content_type':'application/xml'}
     message = self.get_linkedin_post_data(request)
     raw_body = '<activity locale="en_US"><content-type>linkedin-html</content-type><body>%s</body></activity>' % message
     oauth.fetchResponse(oauthRequest, network.base_url, headers=headers, raw_body=raw_body)
    def twitter(self, request, network, response):
        """
        Helper function to handle the callbacks for twitter
        """
        # The first step is to make sure there is an unauthed_token in the session, and that it matches the one
        # the provider gave us back
        unauthed_token = request.session.get('%s_unauthed_token' % network.name, None)
        if not unauthed_token:
            return response.send(errors=NETWORK_HTTP_ERROR % network.name)
        request.session['%s_unauthed_token' % network.name]
        requestToken = oauth.OAuthToken.from_string(unauthed_token)

        # The below ugly hack is brought to you by piston (removes oauth_ keys from request.GET )and urlparse
        # (places querystring values in lists??)
        if requestToken.key != urlparse.parse_qs(request.META['QUERY_STRING']).get('oauth_token', [None, ])[0]:
            return response.send(errors=NETWORK_HTTP_ERROR % network.name)
        verifier = urlparse.parse_qs(request.META['QUERY_STRING']).get('oauth_verifier', [None, ])[0]

        #Now we are building a request, so we can exchange this unauthed token for an access_token
        oauthRequest = oauth.makeOauthRequestObject(network.getAccessTokenURL(), network.getCredentials(),
                                                    token=requestToken, verifier=verifier)
        accessToken = oauth.fetchResponse(oauthRequest, network.base_url).read()

        try:
            oauth.OAuthToken.from_string(accessToken)
        except KeyError:
            return response.send(errors=NETWORK_HTTP_ERROR % network.name)

        #store the token in the session and in the db, in the future we will look in the session first, and then
        #the db if that fails
        request.session['%s_access_token' % network.name] = accessToken
        params = urlparse.parse_qs(accessToken)

        if network.name == 'linkedin':
            oauthRequest = oauth.makeOauthRequestObject('https://%s/v1/people/~' % network.base_url,
                                network.getCredentials(), token=oauth.OAuthToken.from_string(accessToken),
                                method='GET')
            ret = oauth.fetchResponse(oauthRequest, network.base_url).read()
            ret = utils.fromXML(ret)
            params['user_id'] = [urlparse.parse_qs(ret['site_standard_profile_request']['url'])['key'][0], '0']
            params['screen_name'] = ['%s %s' % (ret['first_name'], ret['last_name']), '0']

        network_dict = {}
        network_dict['access_token'] = accessToken
        network_dict['uuid'] = params['user_id']
        network_dict['name_in_network'] = params['screen_name'][0]
        network_dict['network'] = network

        self.finish_callback(request, response, network_dict=network_dict)
 def linkedin(self, profile, network, credentials):
     oauthRequest = oauth.makeOauthRequestObject('https://%s/v1/people/~/connections' % network.base_url,
                                                 network.getCredentials(), method='GET',
                                                 token=oauth.OAuthToken.from_string(credentials.token))
     ret = oauth.fetchResponse(oauthRequest, network.base_url).read()
     friends = utils.fromXML(ret).person
     return [urlparse.parse_qs(y['site_standard_profile_request']['url'])['key'][0] for y in friends if y['site_standard_profile_request']]
 def twitter(self, profile, network, credentials):
     oauthRequest = oauth.makeOauthRequestObject('https://%s/1/statuses/friends.json' % network.base_url,
                                                 network.getCredentials(),
                                                 token=oauth.OAuthToken.from_string(credentials.token))
     ret = oauth.fetchResponse(oauthRequest, network.base_url).read()
     friends = simplejson.loads(ret)
     return [x['id'] for x in friends]
    def twitter(self, request, network, response):
        """
        Helper function to handle twitter redirect
        """
        #the first step is a an unauthed 'request' token, the provider will not even deal with us until we have that
        # so we build a request, and sign it,
        tokenRequest = oauth.makeOauthRequestObject(network.getRequestTokenURL(), network.getCredentials(),
                                                    callback=network.getCallBackURL(request), method='POST')

        result = oauth.fetchResponse(tokenRequest, network.base_url).read()
        try:
            token = oauth.OAuthToken.from_string(result)
        except KeyError:
            if 'whale' in result:
                return HttpResponse(result)
            return response.send(errors=result)

        # save the token to compare to the one provider will send back to us
        request.session['%s_unauthed_token' % network.name] = token.to_string()

        # we needed the token to form the authorization url for the user to go to
        # so we build the oauth request, sign it, and use that url
        oauthRequest = oauth.makeOauthRequestObject(network.getAuthURL(), network.getCredentials(), token=token)

        #finally, redirect the user to the url we've been working so hard on
        request.session['from_url'] = request.META.get('HTTP_REFERER', '/')

        return HttpResponseRedirect(oauthRequest.to_url())
 def twitter(self, request, credentials, network):
     message = self.get_twitter_post_data(request)
     oauthRequest = oauth.makeOauthRequestObject('https://%s/1/statuses/update.json' % network.base_url,
                                                 network.getCredentials(), token=oauth.OAuthToken.from_string(credentials.token),
                                                 method='POST', params={'status': message})
     oauth.fetchResponse(oauthRequest, network.base_url)