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://dummy.dummy/nonexistent.ico
Photo Title 2
http://dummy.dummy/nonexistent.ico
Photo Title 3
http://dummy.dummy/nonexistent.ico
Photo Title 4
http://dummy.dummy/nonexistent.ico
Photo Title 5
http://dummy.dummy/nonexistent.ico
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