Objective-C GMail: Get User's GMail Profile

Back to Index

Gets the current user's Gmail profile.

Documentation: https://developers.google.com/gmail/api/v1/reference/users/getProfile


#import <CkoRest.h>
#import <CkoOAuth2.h>
#import <CkoStringBuilder.h>
#import <CkoJsonObject.h>
#import <NSString.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

//   Provide a previously obtained OAuth2 access token.
CkoOAuth2 *oauth2 = [[CkoOAuth2 alloc] init];
oauth2.AccessToken = @"OAUTH2_ACCESS_TOKEN";
[rest SetAuthOAuth2: oauth2];

success = [rest Connect: @"www.googleapis.com" port: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

[rest AddPathParam: @"userId" value: @"matt@chilkat.io"];

CkoStringBuilder *sbJson = [[CkoStringBuilder alloc] init];
success = [rest FullRequestNoBodySb: @"GET" uriPath: @"/gmail/v1/users/userId/profile" sb: sbJson];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"Received error response code: ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@",@"Response body:");
    NSLog(@"%@",[sbJson GetAsString]);
    return;
}

CkoJsonObject *json = [[CkoJsonObject alloc] init];
[json LoadSb: sbJson];

//  The following code parses the JSON response.
//  A sample JSON response is shown below the sample code.
NSString *emailAddress = 0;
int messagesTotal;
int threadsTotal;
NSString *historyId = 0;

emailAddress = [json StringOf: @"emailAddress"];
messagesTotal = [[json IntOf: @"messagesTotal"] intValue];
threadsTotal = [[json IntOf: @"threadsTotal"] intValue];
historyId = [json StringOf: @"historyId"];

NSLog(@"%@",@"Example Completed.");

Sample JSON Response Body

{
  "emailAddress": "matt@chilkat.io",
  "messagesTotal": 3,
  "threadsTotal": 3,
  "historyId": "1257"
}