Link MCC to Ads account using REST interface

63 views
Skip to first unread message

Tomas Maccarone

unread,
May 15, 2024, 11:50:39 AM5/15/24
to Google Ads API and AdWords API Forum
Hi,
Im trying to connect an MCC account to a Google Ads account following the following link https://developers.google.com/google-ads/api/rest/reference/rest/v16/customers.accountLinks/create

export async function createAccountLink(
managerCustomerId,
clientCustomerId,
clientAccessToken
) {
const url = `https://googleads.googleapis.com/v16/customers/${managerCustomerId}/accountLinks:create`;

// The payload for the POST request
const body = {
accountLink: {
status: "PENDING_APPROVAL",
},
};

try {
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${clientAccessToken}`, // Bearer token for the client account
"Developer-Token": process.env.GOOGLE_DEV_TOKEN, // Developer token for API access
"Content-Type": "application/json",
"login-customer-id": managerCustomerId, // Manager account ID
},
body: JSON.stringify(body), // Converting the payload to a JSON string
});

if (!response.ok) {
const responseText = await response.text();
console.error(
`Failed to create account link, status: ${response.status}, response: ${responseText}`
);
throw new Error(
`API call failed with status ${response.status}: ${responseText}`
);
}

const data = await response.json();
console.log("Created Account Link:", data);
return data;
} catch (error) {
console.error("Failed to create account link:", error);
throw error;
}
}

I believe I'm missing fields in the body request but I do not understand which ones are required. Do I need to add "type"? What would be the value? "Unknown"? I also couldn't find where to add the CID for the account I'm trying to connect. 

Looking for some guidance here. Appreciate the help!
Thanks!

Google Ads API Forum Advisor

unread,
May 15, 2024, 11:58:08 PM5/15/24
to tomy...@gmail.com, adwor...@googlegroups.com
Hi,

Thank you for reaching out to the Google Ads API support team.

Kindly note that the code you provided is missing some key fields in the request body for creating an account link using the Google Ads API. The missing filed is a type which specifies the type of link being created. While the code includes this information in the headers, it's also recommended to include it in the request body for clarity. I would recommend you refer to the documents AccountLink, Linking product accounts and Linking to Manager Accounts to get more information. Please go through the help center article Create a Google Ads manager account for more details.

I hope this helps. Kindly get back to us if you have any further queries.
 
This message is in relation to case "ref:!00D1U01174p.!5004Q02tIzNw:ref" (ADR-00235173)

Thanks,
 
Google Logo Google Ads API Team


Tomas Maccarone

unread,
May 16, 2024, 4:27:21 PM5/16/24
to Google Ads API and AdWords API Forum
Hi,
Thanks for getting back to me. I understand, I added the fields which I believe were required but I'm still getting the same error, am I missing another field?
export async function createAccountLink(
managerCustomerId,
clientCustomerId,
managerAccessToken
) {
const url = `https://googleads.googleapis.com/v16/customers/${managerCustomerId}/accountLinks:create`;

// The payload for the POST request
const body = {
accountLink: {
type: "UNKNOWN",
status: "PENDING_APPROVAL",
resourceName: `customers/${clientCustomerId}`,
},
};

try {
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${managerAccessToken}`, // Bearer token for the client account
"Developer-Token": process.env.GOOGLE_DEV_TOKEN, // Developer token for API access
"Content-Type": "application/json",
"login-customer-id": managerCustomerId, // Manager account ID
},
body: JSON.stringify(body), // Converting the payload to a JSON string
});

if (!response.ok) {
const responseText = await response.text();
console.error(
`Failed to create account link, status: ${response.status}, response: ${responseText}`
);
throw new Error(
`API call failed with status ${response.status}: ${responseText}`
);
}

const data = await response.json();
console.log("Created Account Link:", data);
return data;
} catch (error) {
console.error("Failed to create account link:", error);
throw error;
}
}

I set type to "UNKNOWN" which is the one I believe it's correct following the available account types from this section: https://developers.google.com/google-ads/api/rest/reference/rest/v16/AccountLink#linkedaccounttype

I'm getting the following error:
Error: API call failed with status 400: {
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.ads.googleads.v16.errors.GoogleAdsFailure",
        "errors": [
          {
            "errorCode": {
              "fieldError": "REQUIRED"
            },
            "message": "The required field was not present.",
            "location": {}
          }
        ],

what field am I missing?

I'm trying to connect an MCC to a Google Ads account. managerCustomerId to clientCustomerId

Thanks for the help!

Google Ads API Forum Advisor

unread,
May 17, 2024, 5:59:57 AM5/17/24
to tomy...@gmail.com, adwor...@googlegroups.com
Hi,

Kindly note that the error message is showing as the 'location' field is missing in your request body. Also, note that resourceName format is invalid. I would recommend you to kindly refer to this documentation for more information. Also, note that you have shared the code but we require the complete API logs (request and response logs with request-id and request header) generated at your end to better assist you further. 

If you are using a client library and haven't enabled the logging yet, I would request you to enable logging for the specific client library that you are using. You can refer to the guides Java, .Net, PHP, Python, Ruby or Perl to enable logging at your end. For REST interface requests, you can enable logging via the curl command by using the -i flag.

You can send the details via Reply privately to the author option, or direct private reply to this email.
Reply all
Reply to author
Forward
0 new messages