Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过ASP.Net获取私有google站点图像的身份验证_C#_Asp.net_Gdata_Google Api Client_Google Sites - Fatal编程技术网

C# 通过ASP.Net获取私有google站点图像的身份验证

C# 通过ASP.Net获取私有google站点图像的身份验证,c#,asp.net,gdata,google-api-client,google-sites,C#,Asp.net,Gdata,Google Api Client,Google Sites,我有一个图片的URL列表,这些图片是从一个私有google站点的内容中获取的(用于获取这些URL的代码部分在这里并不重要,因为它按照预期工作) 有了这些URL,我需要通过使用经过身份验证的get请求(因为google站点是私有的),获取每个URL的流响应(我需要每个URL的本地副本) 这是我验证和生成访问令牌的方式: var certificate = new X509Certificate2("c:/path/to/key.p12", "notasecret", X509KeyStorageF

我有一个图片的URL列表,这些图片是从一个私有google站点的内容中获取的(用于获取这些URL的代码部分在这里并不重要,因为它按照预期工作)

有了这些URL,我需要通过使用经过身份验证的get请求(因为google站点是私有的),获取每个URL的流响应(我需要每个URL的本地副本)

这是我验证和生成访问令牌的方式:

var certificate = new X509Certificate2("c:/path/to/key.p12", "notasecret", X509KeyStorageFlags.Exportable);

var serviceAccountCredentialInitializer =
    new ServiceAccountCredential.Initializer("generated-service-email@developer.gserviceaccount.com")
    {
        Scopes = new[] { "http://sites.google.com/feeds/", 
                         "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.");

_accessToken = credential.Token.AccessToken;
下面是我如何在没有运气的情况下尝试请求(第一个
使用
语句中的错误401):

更新

我试着用谷歌OAuth2.0Playerd做一次尝试。如果我想得到一个饲料,它的工作。但是,如果我试图获取一个映像,它也会提供HTTP/1.1 401未经授权

您需要对自己进行身份验证才能获取该资源。这就是401错误告诉您的。我建议您在捕获http流量时使用浏览器进行身份验证并获取资源,然后将捕获/日志与应用程序创建的http流量进行比较。看看是否有什么不同。@DavidG,这是我验证和获取访问令牌的代码,有什么建议吗?@JohnWu,当我在浏览器上验证并看到图像时,检查流量没有多大帮助,因为标头只有一个cookie(我认为它在做这个把戏)那么,在代码中的什么地方添加cookie呢?
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(imageUrl);
httpWebRequest.Headers.Add("Authorization", String.Format("Bearer {0}", _accessToken));

// error 401 on the next line
using (HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    using (Stream stream = httpWebReponse.GetResponseStream())
    {
        var image = Image.FromStream(stream);
        ...
    }
}