Accessing the Ning API with Python

The Ning API Python library simplifies the code required to interact with the Ning API.

Retrieving tokens

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""
Request the access token for the given member using the Ning API
"""

import oauth2 as oauth
import ningapi

consumer = oauth.Consumer(
        key="0d716e57-5ada-4b29-a33c-2f4af1b26837",
        secret="f0963fa5-1259-434f-86fc-8a17d14b16ca"
        )

host = "external.ningapis.com"
network = "apiexample"

ning_api = ningapi.Client(host, network, consumer)


email = "test@example.com"
password = "I<3Ning"

token = ning_api.login(email, password)

print "Access Key: %s" % token.key
print "Access Secret: %s" % token.secret

Outputs:

Access Key: 30a29805-1fa0-4498-a832-96b46f00bf07
Access Secret: ab67211f-f14f-43b4-971b-81431722a593

Retrieving photos

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Query the Ning API for the five most recent photos, returning the photo's
title and the URL of the image
"""

import oauth2 as oauth
import ningapi

consumer = oauth.Consumer(
        key="0d716e57-5ada-4b29-a33c-2f4af1b26837",
        secret="f0963fa5-1259-434f-86fc-8a17d14b16ca"
        )
token = oauth.Token(
        key="07aa5613-6783-4735-b8f1-4c69642ad438",
        secret="e2c528ec-8b81-402c-8f88-8bf17ba8751f"
        )

host = "external.ningapis.com"
network = "apiexample"

ning_api = ningapi.Client(host, network, consumer, token)


fields = ["title", "image.url"]
attrs = {
    "fields": ",".join(fields),
    "count": 5
}

content = ning_api.get("Photo/recent", attrs)
for photo in content["entry"]:
    photo_id = photo["id"]
    photo_resource = content["resources"][photo_id]

    print "%s\n\t%s" % (photo["title"], photo_resource["url"])

Outputs:

Photo Title 1
	http://api.ning.com:80/files/v-cOIk8putiKMSg0Bi8m3zM5oLTKyMJ*sB5Mza2zijWo7ko9qrPwA38cx0evq*LSCn-bGE*ZVjn*PNpWxYvOHF32UQyrDpi/upload55740.tmp
Photo Title 2
	http://api.ning.com:80/files/FA1Wz3cE6XK9cOnGAzbckgX8DbbF-D-S*mqwEEljZZkE9snrAS0tiXoLnYC8-n3QMMMmrgpAVcVboEAh85Z-IEqh4GVvz5J/upload57587.tmp
Photo Title 3
	http://api.ning.com:80/files/5jv2LhKnz09*0EUKWctnCAPJTtRbALYpYqvIEzUcq3xfadrB5EMtLL7ieMslr7yyCxb7stDbR5W3cmM0Cyp39eDLN01pjMX/upload4541.tmp
Photo Title 4
	http://api.ning.com:80/files/-OpCvgGDYR3T6wvL-uDQr3dXHR*TwJv-wNIrmKE1A2kDF7c7rODetxNgmpT1hXU6ApNT5sAdNUY5C*D3rVGQ8pxEF30pGMM/upload41901.tmp
Photo Title 5
	http://api.ning.com:80/files/0xSW8BNOLVbZBmP8m86Grid3-8nAx0iUJKgH-qHbx37eB6jj-QhbZdgegtpvbgdzrWg-hl*0-jdImK1ty4yWqwZspCUQ2Y3/upload41900.tmp

Add a new photo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Upload a photo using the Ning API
"""

import oauth2 as oauth
import ningapi

consumer = oauth.Consumer(
        key="0d716e57-5ada-4b29-a33c-2f4af1b26837",
        secret="f0963fa5-1259-434f-86fc-8a17d14b16ca"
        )
token = oauth.Token(
        key="07aa5613-6783-4735-b8f1-4c69642ad438",
        secret="e2c528ec-8b81-402c-8f88-8bf17ba8751f"
        )

host = "external.ningapis.com"
network = "apiexample"

ning_api = ningapi.Client(host, network, consumer, token)


photo_title = "Photo Title"
photo_desc = "Photo Description"
photo_path = "/Users/devin/Pictures/nasa/NASA-23.jpg"
photo_content_type = "image/jpeg"

photo_fields = {
        "title": photo_title,
        "description": photo_desc,
        "file": photo_path,
        "content_type": photo_content_type
        }

content = ning_api.post("Photo", photo_fields)

if content["success"]:
    print "Photo uploaded: %s" % content["id"]

Outputs:

Photo uploaded: 3011345:Photo:3932

Update a photo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Update a photo using the Ning API
"""

import oauth2 as oauth
import ningapi

consumer = oauth.Consumer(
        key="0d716e57-5ada-4b29-a33c-2f4af1b26837",
        secret="f0963fa5-1259-434f-86fc-8a17d14b16ca"
        )
token = oauth.Token(
        key="07aa5613-6783-4735-b8f1-4c69642ad438",
        secret="e2c528ec-8b81-402c-8f88-8bf17ba8751f"
        )

host = "external.ningapis.com"
network = "apiexample"

ning_api = ningapi.Client(host, network, consumer, token)


photo_title = "Updated Photo Title"
photo_desc = "Updated Photo Description"

photo_fields = {
        "title": photo_title,
        "description": photo_desc,
        "id": "3011345:Photo:3930"
        }

content = ning_api.put("Photo", photo_fields)

if content["success"]:
    print "Photo updated"

Outputs:

Photo updated

Delete a photo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Delete a photo using the Ning API
"""

import oauth2 as oauth
import ningapi

consumer = oauth.Consumer(
        key="0d716e57-5ada-4b29-a33c-2f4af1b26837",
        secret="f0963fa5-1259-434f-86fc-8a17d14b16ca"
        )
token = oauth.Token(
        key="07aa5613-6783-4735-b8f1-4c69642ad438",
        secret="e2c528ec-8b81-402c-8f88-8bf17ba8751f"
        )

host = "external.ningapis.com"
network = "apiexample"

ning_api = ningapi.Client(host, network, consumer, token)



photo_fields = {
        "id": "3011345:Photo:3928"
        }

content = ning_api.delete("Photo", photo_fields)

if content["success"]:
    print "Photo deleted"

Outputs:

Photo deleted