Gabor85
Gabor85

Reputation: 135

YouTube API - The API Key and the authentication credential are from different projects

I am trying to get videos from youtube with my C# application, with "https://youtube.googleapis.com/youtube/v3/search?forMine=true&order=date&type=video&part=snippet&key={key}&access_token={token}" but I receive this error:

"error": {
    "code": 400,
    "message": "The API Key and the authentication credential are from different 
projects.",
    "errors": [
      {
        "message": "The API Key and the authentication credential are from 
different projects.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": 
"https://console.developers.google.com/project/{MyProjectCode}/apiui/credential"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CONSUMER_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "consumer": "projects/{MyProjectCode}",
          "service": "youtube.googleapis.com"
        }
      }
    ]
  }

But I can see only 1 API Key and 1 OAuth 2.0 Client ID (with Client ID and Client Secret) on https://console.cloud.google.com/apis/api/youtube.googleapis.com/credentials?authuser=1&project={MyProject} site

Could you please help how can I fix this ?

Code:

var token = Helper.Decrypt(ytConn.AccessToken);
var url = "https://youtube.googleapis.com/youtube/v3/search? 
forMine=true&order=date&type=video&part=snippet&key=" + _key + 
"&access_token=" + token;

videos = Helper.GetFromAPI<Videos>(url, token);

it calls this:

public static T GetFromAPI<T>(string url, string token)
{            
    WebRequest request = WebRequest.Create(url);
    request.Method = "GET";

    ServicePointManager.SecurityProtocol = 
    SecurityProtocolType.Tls12;
    request.Headers.Set("Authorization", "Bearer " + token);                

    request.ContentType = "application/json; charset=utf-8";

    T type = default(T);

    try
    {
        using (WebResponse response = request.GetResponse())
        {
            using (Stream dataStream = response.GetResponseStream())
            {
                using (StreamReader reader = new 
                StreamReader(dataStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    type = JsonConvert.DeserializeObject<T> 
                    (responseFromServer);
                }
            }
        }
    }
}

Thanks in advance

Upvotes: 1

Views: 1637

Answers (2)

Gabor85
Gabor85

Reputation: 135

There was no need to change the code, on one day started working again, so most probably the issue was on Youtube side.

Upvotes: 0

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 117261

You have added an API key, API keys are needed for accessing public data. They are really only needed to identify your project to Google. You have also included an access token in your request. Access tokens are used to access private user data. Access tokens also identify your project so technically speaking you dont need both.

The error message you are getting

The API Key and the authentication credential are from different projects

means that the API key you are using is from one project on Google cloud console and the access token was created using the client credentials from a different project on google cloud console. While you dont need both there is nothing stopping you from using both other then the fact that they need to be from the same project.

I subject that you just remove the key section from your code. if you are sending an access token there is no need to have key.

https://youtube.googleapis.com/youtube/v3/search? 
forMine=true&order=date&type=video&part=snippet + 
"&access_token=" + token;

You should consider using the Google api .net client library.

Upvotes: 0

Related Questions