0

In google, images are hosted in CDN URL type and I tried to download as an image from that CDN but it throws an error in C#. Used this c# code attached below.

          using (var webClient = new WebClient())
            {
                byte[] imageBytes = webClient.DownloadData(imageUUl);
                System.IO.File.WriteAllBytes(@"E:\Temp\img2.jpeg", imageBytes);
            }

URL: https://lh6.googleusercontent.com/vpsleVfq12ZnALrwbIUqCTa0Fpqa5C8IUViGkESOSqvHshQpKCyOq4wsRfTcadG2WYgcW3m0yq_6M2l_IrSM3qr35spIML9iyIHEULwRu4mWw4CUjCwpVfiWnd5MXPImMw=w1280

Thanks in advance.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • What is the error you are getting? – chrisbyte Feb 14 '22 at 14:37
  • This is the error I got from code "The remote server returned an error: (403) Forbidden." but that URL work once only. when i refresh in browser then URL will be changes as lh6.googleusercontent and not worked in code after refreshed – Aravind Elumalai Feb 14 '22 at 14:43
  • 403. That’s an error. Your client does not have permission to get URL /vpsleVfq12ZnALrwbIUqCTa0Fpqa5C8IUViGkESOSqvHshQpKCyOq4wsRfTcadG2WYgcW3m0yq_6M2l_IrSM3qr35spIML9iyIHEULwRu4mWw4CUjCwpVfiWnd5MXPImMw=w1280 from this server. (Client IP address: 14.142.184.18) – Aravind Elumalai Feb 14 '22 at 14:45

2 Answers2

2

In GCP Cloud CDN, you can use a signed URL with authentication or signed cookies to authorize users and provide them with a time-limited token for accessing your protected content, so a Cloud CDN does not block requests without a signature query parameter or Cloud-CDN-Cookie HTTP cookie. It rejects requests with invalid (or otherwise malformed) request parameters, due to this I suggest reviewing your browser client security settings; how the authentication is managed to your CDN URL. Some clients store cookies by default if the security policy is allowed, also review how your CDN URL security ingress is configured because when you are using a CDN URLs with signed cookies the responses to signed and unsigned requests are cached separately, so a successful response to a valid signed request is never used to serve an unsigned request. In another hand if you are using a signed CDN URL to limited secure access to file for a limited amount of time, there are some steps that you need to follow first:

  • Ensure that Cloud CDN is enabled.

  • If necessary, update to the latest version of the Google Cloud CLI:

                   `gcloud components update`
    
  • Creating keys for your signed URLs

    To create keys, follow these steps.

          1.In the Google Cloud Console, go to the Cloud CDN page.
          2.Click Add origin. 
          3.Select an HTTP(S) load balancer as the origin.
          4.Select backend services or backend buckets. For each one
    
             -Click Configure, and then click Add signing key.
    
             -Under Name, give the new signing key a name.
    
             -Under the Key creation method, select Automatically generate or Let me enter.
    
            - If you're entering your own key, type the key into the text field.
    
            - Click Done.
    
            - Under Cache entry maximum age, provide a value, and select a Unit of time from the drop-down list. You can choose among second, minute, hour, and day. The maximum amount of time is three (3) days.
    
         5. Click Save.
         6. Click Add.
    
  • Configuring Cloud Storage permissions.

Before you run the following command, add at least one key to a backend bucket in your project; otherwise, the command fails with an error because the Cloud CDN cache fill service account is not created until you add one or more keys for the project. Replace PROJECT_NUM with your project number and BUCKET with your storage bucket.

gsutil iam ch \ serviceAccount:service-PROJECT_NUM@cloud-cdn-fill.iam.gserviceaccount.com:objectViewer \ gs://BUCKET

  • List the keys on a backend service or backend bucket, run one of the following commands:

    gcloud compute backend-services describe BACKEND_NAME

    gcloud compute backend-buckets describe BACKEND_NAME

  • Sign URLs and distribute them.

You can sign URLs by using the gcloud compute sign-url command or by using code that you write yourself. If you need many signed URLs, custom code provides better performance. This command reads and decodes the base64url encoded key value from KEY_FILE_NAME, and then outputs a signed URL that you can use for GET or HEAD requests for the given URL.

gcloud compute sign-url \
  "https://example.com/media/video.mp4" \
  --key-name my-test-key \
  --expires-in 30m \
  --key-file sign-url-key-file

In this link, you can find more info related to signed URLs and signed cookies.

Leo
  • 695
  • 3
  • 11
0

You can't download an image in that way, since you need to provide an OAuth token. and you need to have the profile scope enabled

    var GoogleAuth; // Google Auth object.
function initClient() {
  gapi.client.init({
      'apiKey': 'YOUR_API_KEY',
      'clientId': 'YOUR_CLIENT_ID',
      'scope': 'https://www.googleapis.com/auth/userinfo.profile',
      'discoveryDocs': ['https://discovery.googleapis.com/discovery/v1/apis']
  }).then(function () {
      GoogleAuth = gapi.auth2.getAuthInstance();

      // Listen for sign-in state changes.
      GoogleAuth.isSignedIn.listen(updateSigninStatus);
  });
}
David Salomon
  • 804
  • 1
  • 7
  • 24