Dart
Dart
Paging User Photos with Cursor
See more Facebook Examples
Demonstrates how to iterate over the pages of user photos using a cursor.Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// This example assumes a previously obtained an access token
final oauth2 = CkOAuth2();
oauth2.accessToken = 'FACEBOOK-ACCESS-TOKEN';
final rest = CkRest();
// Connect to Facebook.
try {
rest.connect('graph.facebook.com', 443, true, true);
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
// Provide the authentication credentials (i.e. the access key)
rest.setAuthOAuth2(oauth2);
// Indicate that we only want the photos the user has personally uploaded.
rest.addQueryParam('type', 'uploaded');
// We could limit the number of photos per page using the "limit" field.
rest.addQueryParam('limit', '20');
// Get the 1st page of photos. (Not the actual image data, but the information about each photo.)
// See https://developers.facebook.com/docs/graph-api/reference/user/photos/ for more information.
String responseJson;
try {
responseJson = rest.fullRequestNoBody('GET', '/v2.7/me/photos');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
final json = CkJsonObject();
json.emitCompact = false;
json.load(responseJson);
print(json.emit());
// See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
//
// Get the "after" cursor.
var afterCursor = json.stringOf('paging.cursors.after');
while (json.lastMethodSuccess) {
print('after cursor: $afterCursor');
// Prepare for getting the next page of photos.
// We can continue using the same REST object.
// If already connected, we'll continue using the existing connection.
// Otherwise, a new connection will automatically be made if needed.
rest.clearAllQueryParams();
rest.addQueryParam('type', 'uploaded');
rest.addQueryParam('limit', '20');
rest.addQueryParam('after', afterCursor);
try {
responseJson = rest.fullRequestNoBody('GET', '/v2.7/me/photos');
} on ChilkatException catch (e) {
print(e.lastErrorText);
return;
}
json.load(responseJson);
// See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
print(json.emit());
// Get the cursor for the next page.
afterCursor = json.stringOf('paging.cursors.after');
}
print('No more pages of photos.');
}