Click here to Skip to main content
14,422,668 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to access google sites feed by using google service account. But I get "The remote server returned an error: (403) Forbidden."(Not authorized to access this feed) error.

The code written is as follows:

var certificate = new X509Certificate2(filePath, password, X509KeyStorageFlags.Exportable);

var serviceAccountCredentialInitializer =new ServiceAccountCredential.Initializer("abc@developer.gserviceaccount.com")
{Scopes = new[] {"https://sites.google.com/feeds/"}}.FromCertificate(certificate);

var credential = new ServiceAccountCredential(serviceAccountCredentialInitializer);

if(!credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result)

throw new InvalidOperationException("Access token request failed.");

var requestFactory = new GDataRequestFactory(null);
requestFactory.CustomHeaders.Add("Authorization: Bearer " + credential.Token.AccessToken);

//SitesService class inherits MediaService

 SitesService service = new SitesService(null)
            {
                RequestFactory = requestFactory 
            };

Stream feed = service.Query(new Uri("https://sites.google.com/feeds/content/domain-name/site-name?path=page-path"));


I have also granted access to required scopes to google service account from admin panel. But still same error.

Can someone please tell me, what is wrong in my code?
Posted
Updated 13-Nov-15 20:41pm
v2
Comments
Afzaal Ahmad Zeeshan 14-Nov-15 2:31am
   
Did you pass the password too? Your code only shows the username being passed.
Member 12138450 15-Nov-15 7:21am
   
I have passed password to the certificate.
Can we pass password to google service account?
Do we get password for google service account?
ZurdoDev 15-Nov-15 10:25am
   
Seems like you should be asking google some of these questions.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100