React Native
React Native
Paging User Photos with Cursor
See more Facebook Examples
Demonstrates how to iterate over the pages of user photos using a cursor.Chilkat React Native Downloads
import { JsonObject, OAuth2, Rest } from '@chilkat/react-native'
async function chilkatExample() {
// 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
const oauth2 = new OAuth2();
oauth2.accessToken = 'FACEBOOK-ACCESS-TOKEN';
const rest = new Rest();
// Connect to Facebook.
try {
await rest.connectAsync('graph.facebook.com', 443, true, true);
} catch {
console.log(rest.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.
let responseJson: string;
try {
responseJson = await rest.fullRequestNoBodyAsync('GET', '/v2.7/me/photos');
} catch {
console.log(rest.lastErrorText);
return;
}
const json = new JsonObject();
json.emitCompact = false;
json.load(responseJson);
console.log(json.emit());
// See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
//
// Get the "after" cursor.
let afterCursor = json.stringOf('paging.cursors.after');
while (json.lastMethodSuccess) {
console.log(`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 = await rest.fullRequestNoBodyAsync('GET', '/v2.7/me/photos');
} catch {
console.log(rest.lastErrorText);
return;
}
json.load(responseJson);
// See Parsing the Facebook User Photos for code showing how to parse the JSON photos content.
console.log(json.emit());
// Get the cursor for the next page.
afterCursor = json.stringOf('paging.cursors.after');
}
console.log('No more pages of photos.');
}