Sample code for 30+ languages & platforms
Swift

Google Search Console API - List

See more Google Search Console Examples

Lists the user's Search Console sites.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // This example uses a previously obtained access token having permission for the 
    // Google Search Console scope.

    // In this example, Get a Google Search Console OAuth2 Access Token, the access
    // token was saved to a JSON file.  This example fetches the access token from the file..
    let jsonToken = CkoJsonObject()!
    success = jsonToken.loadFile(path: "qa_data/tokens/googleSearchConsole.json")
    if jsonToken.hasMember(jsonPath: "access_token") == false {
        print("No access token found.")
        return
    }

    let http = CkoHttp()!
    http.authToken = jsonToken.string(of: "access_token")

    var responseStr: String? = http.quickGetStr(url: "https://www.googleapis.com/webmasters/v3/sites")
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    var statusCode: Int = http.lastStatus.intValue
    print("Response Status Code: \(statusCode)")

    // Sample response:

    // {
    //  "siteEntry": [
    //   {
    //    "siteUrl": "https://www.example.com/",
    //    "permissionLevel": "siteUnverifiedUser"
    //   },
    //   {
    //    "siteUrl": "http://www.chilkatsoft.com/",
    //    "permissionLevel": "siteOwner"
    //   }
    //  ]
    // }

    print("\(responseStr!)")

    // Use this online tool to generate parsing code from sample JSON: 
    // Generate Parsing Code from JSON

    let json = CkoJsonObject()!

    success = json.load(json: responseStr)

    var siteUrl: String?
    var permissionLevel: String?

    var i: Int = 0
    var count_i: Int = json.size(ofArray: "siteEntry").intValue
    while i < count_i {
        json.i = i
        siteUrl = json.string(of: "siteEntry[i].siteUrl")
        print("siteUrl: \(siteUrl!)")
        permissionLevel = json.string(of: "siteEntry[i].permissionLevel")
        print("permissionLevel: \(permissionLevel!)")
        i = i + 1
    }


}