DataFlex Requires Chilkat v11.0.0+
DataFlex
Find a Gmail label ID by name
See more GMail REST API Examples
Look up a label using its exact display name, including any parent/child path. Labels and their IDs belong to the authenticated mailbox. Use Gmail’s returned ID when applying a user label to messages.
Authorize the application with https://www.googleapis.com/auth/gmail.labels. The example reads the access token from gmail-token.json in a private working directory. Authorize a desktop application.
Chilkat DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoToken
Handle hoHttp
Variant vResp
Handle hoResp
Handle hoReply
Variant vLabel
Handle hoLabel
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatJsonObject)) To hoToken
If (Not(IsComObjectCreated(hoToken))) Begin
Send CreateComObject of hoToken
End
Get ComLoadFile Of hoToken "gmail-token.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read gmail-token.json from the private working directory."
Procedure_Return
End
Get ComJsonTypeOf Of hoToken "access_token" To iTemp1
Get ComStringOfEquals Of hoToken "access_token" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "An OAuth access token is required."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get ComStringOf Of hoToken "access_token" To sTemp1
Set ComAuthToken Of hoHttp To sTemp1
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" "https://gmail.googleapis.com/gmail/v1/users/me/labels" vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "Gmail did not return an HTTP response."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
Get ComStatusCode Of hoResp To iTemp2
If ((iSuccess <> True) Or (iTemp1 < 200) Or (iTemp2 >= 300)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Gmail HTTP " iTemp1
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess = True) Begin
Get ComStringOf Of hoReply "error.message" To sTemp1
Showln sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a Gmail JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "labels" To iTemp1
If (iTemp1 <> 4) Begin
Showln "Expected a list of labels."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoLabel
If (Not(IsComObjectCreated(hoLabel))) Begin
Send CreateComObject of hoLabel
End
Get pvComObject of hoLabel to vLabel
Get ComFindRecord2 Of hoReply "labels" "name" "To review" True vLabel To iSuccess
If (iSuccess <> True) Begin
Showln "No label with that exact name. Check the name or create the label first."
Procedure_Return
End
Get ComStringOf Of hoLabel "id" To sTemp1
Showln "Label ID: " sTemp1
End_Procedure