Giter Club home page Giter Club logo

uservoice-python's Introduction

โ›” [DEPRECATED] This repository is no longer actively maintained. You may fork the project but we are no longer accepting pull requests or new issues. You may reach out to [email protected] with questions.

UserVoice Python module for API connections

This library allows you to easily:

  • Generate SSO token for creating SSO users / logging them into UserVoice (http://uservoice.com).
  • Do 3-legged and 2-legged UserVoice API calls safely without having to worry about the cryptographic details (unless you want).

Installation

Install the uservoice Python module from PyPI:

pip install uservoice

or get the source from (http://pypi.python.org/pypi/uservoice) and use the setup.py method:

python setup.py install

Now you should be good to go!

Examples

Prerequisites:

# Suppose your UserVoice site is at http://uservoice-subdomain.uservoice.com/
USERVOICE_SUBDOMAIN = 'uservoice-subdomain'
SSO_KEY = '982c88f2df7257859e8e23423eg87ed' # Admin Console: Settings -> General -> User Authentication

# Define an API client at: Admin Console -> Settings -> Channels -> API
API_KEY = 'oQt2BaunWNuainc8BvZpAm'
API_SECRET = '3yQMSoXBpAwuK3nYHR0wpY6opE341inL9a2HynGF2'

SSO-token generation using the uservoice python library

SSO-token can be used to create sessions for SSO users. They are capable of synchronizing the user information from one system to another. Generating the SSO token from SSO key and given uservoice subdomain can be done by calling UserVoice.generate_sso_token method like this:

import uservoice

sso_token = uservoice.generate_sso_token(USERVOICE_SUBDOMAIN, SSO_KEY, {
    'guid': 1000000,
    'display_name': "User Name",
    'email': '[email protected]'
}, 5*60) # the token will be valid for 5 minutes (5*60 seconds) by default

print "https://" + USERVOICE_SUBDOMAIN + ".uservoice.com/?sso=" + sso_token

Making API calls

You need to create an instance of uservoice.Client. Get API_KEY and API_SECRET for an API client which you can create from Admin Console. Go to Settings -> Channels -> API.

import uservoice
try:
    client = uservoice.Client(USERVOICE_SUBDOMAIN, API_KEY, API_SECRET)

    # Get users of a subdomain (requires trusted client, but no user)
    users = client.get_collection("/api/v1/users")

    print "Subdomain \"{subdomain}\" has {total} users".format(subdomain=USERVOICE_SUBDOMAIN,
                                                               total=len(users))
    for user in users:
        print 'User: "{name}", Profile URL: {url}'.format(**user)

    # Now, let's login as [email protected], a regular user
    with client.login_as('[email protected]') as regular_access_token:
        # Example request #1: Get current user.
        user = regular_access_token.get("/api/v1/users/current")['user']

    print 'User: "{name}", Profile URL: {url}'.format(**user)

    # Login as account owner
    owner_access_token = client.login_as_owner()

    # Example request #2: Create a new private forum limited to only example.com email domain.
    forum = owner_access_token.post("/api/v1/forums", {
        'forum': {
            'name': 'Python Client Private Feedback',
            'private': True,
            'allow_by_email_domain': True,
            'allowed_email_domains': [{'domain': 'example.com'}]
        }
     })['forum']

    print 'Forum "{name}" created! URL: {url}'.format(**forum)
except uservoice.Unauthorized as e:
    # Thrown usually due to faulty tokens, untrusted client or if attempting
    # operations without Admin Privileges
    print e
except uservoice.NotFound as e:
    # Thrown when attempting an operation to a resource that does not exist
    print e

Verifying a UserVoice user

If you want to make calls on behalf of a user, but want to make sure he or she actually owns certain email address in UserVoice, you need to use 3-Legged API calls. Just pass your user an authorize link to click, so that user may grant your site permission to access his or her data in UserVoice.

import uservoice
CALLBACK_URL = 'http://localhost:3000/' # your site

client = uservoice.Client(USERVOICE_SUBDOMAIN, API_KEY, API_SECRET, callback=CALLBACK_URL)

# At this point you want to print/redirect to client.authorize_url in your application.
# Here we just output them as this is a command-line example.
print "1. Go to {url} and click \"Allow access\".".format(url=client.authorize_url())
print "2. Then type the oauth_verifier which is passed as a GET parameter to the callback URL:"

# In a web app we would get the oauth_verifier via a redirection to CALLBACK_URL.
# In this command-line example we just read it from stdin:
access_token = client.login_with_verifier(raw_input())

# All done. Now we can read the current user's email address:
user = access_token.get("/api/v1/users/current")['user']
print "User logged in, Name: {name}, email: {email}".format(**user)

# To reuse the access token at a later point, store the token and secret. For example:
# 1. Find user (in Django)
u = User.objects.get(email=user['email'])
# 2. Associate token and secret with the user
u.access_tokens.create(system='uservoice', token=access_token.token, secret=access_token.secret)

# When you need the token again:
# 1. Find token of the current user
token = u.access_tokens.get(system='uservoice')
# 2. Use the token and secret to log in
access_token = client.login_with_access_token(token.token, token.secret)

uservoice-python's People

Contributors

austintaylor avatar bzwheeler avatar cypriss avatar flebel avatar jcamenisch avatar jdufresne avatar raimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

uservoice-python's Issues

requests GET doesn't include params

The current implementation of "GET" using requests does not include any params or data:

https://github.com/uservoice/uservoice-python/blob/master/uservoice/client.py#L88

However, the user needs to be able to set parameters (eg, per_page, status) for many of the API calls, such as getting a ticket:

https://developer.uservoice.com/docs/api/reference/#_api_v1_tickets_get

Is there a reason this is not exposed, or is there another way that the python sdk is intended to be used? Thanks!

ImportError: No module named 'sso'

Mac OS 10.10
Python 3.4


  1. I installed uservoice as described here (https://github.com/uservoice/uservoice-python):
    pip install uservoice
  2. Simple "import uservoice" fires error:
    Traceback (most recent call last):
    File "", line 1, in
    File "python3.4/site-packages/uservoice/init.py", line 1, in
    from sso import generate_sso_token
    ImportError: No module named 'sso'

django import

Hello, thanks for your great work.

How can I use this module in django?

Error with Requests 2.18.4 or Urrlib3 1.22

The code below stopped working with Requests 2.18.4 and Urrlib3 1.22. It was working fine with Requests 2.11.1 and Urllib3 1.7.1:

uservoice.get("/api/v1/support_queues.json?per_page=500")["support_queues"]

Requests now throws an ApplicationError with this information:

{u'errors': {u'message': u'Internal Error. Make sure you passed valid arguments', u'type': u'application_error'}}

Including the parameters in the path was a workaround around your library not supporting parameters for gets (#11). I think Requests plugged the workaround, so now #11 is very urgent

Choose a license for this package

Please specify a license in the package's setup.py.
I was told on Oct 30, 2015 that the package is licensed under the MIT license, but still nothing in the package states that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.