如何使用 C# 对 Picasa 网络相册数据 API 进行 OAuth2 身份验证?

How can I make OAuth2 authentication to the Picasa Web Albums Data API with C#?

我正在尝试使用 C# 中的 Picasa Web Albums Data API 上传照片,我正在使用 RestSharp 来帮助我处理 HTTP 请求。

我有客户端 ID 和客户端密码,但我不知道如何将其放在 http 请求中以进行身份​​验证。

我遇到错误:

Modification only allowed with api authentication.

返回的 StatusCode 是 Forbidden

这是我的代码,其中是我的用户 ID,是我的相册 ID。

方法 "Authenticantion()" returns 我已经有一个令牌。

[HttpPost]
public string PostImage(){
    var restclient = new RestClient(BaseUrl);
    RestRequest request = new RestRequest("https://picasaweb.google.com/data/feed/api/user/<userID>/albumid/<albumID>") {Method = Method.POST};

    string imageBase64 = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAIUAyAMBEQACEQEDEQH/xAAcAAABBQEBAQAAAAAAAAAAAAAAAQIDBAUGBwj/xAA6EAABAwIDBgMFBgYDAQAAAAABAAIDBBEFITESE0FRYXEGIpEUMkJSgSMzYqGx0QdDgpLB4TVTchX/xAAaAQEAAwEBAQAAAAAAAAAAAAAAAQIDBQQG/8QAMREAAgIBAwMDAgMIAwAAAAAAAAECAxEEEjETIUEFMlEiYRRCkSMzUoGhsdHhNHHx/9oADAMBAAIRAxEAPwD3FACAEAhNkA3eM29jbbt2vs3zsoyuAOupAqAEAIAQAgBACAEAIAQAgBACAEAIAQAgBACAEAIAQHmuNfxbw6mdUx4dRzVIieWNqLjdvIyJGdyL8eKxnbjsuT1Vafd3lweTYx4sbisrq+eB7nSEh0r7B3qNOnJYKpuXd9z2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1PPLX1L29zSpsHtYyuHYBdCj0iivvL6meSzW2S9vY2KWBkQ2WNsF1UsLCPHk0IhZATDRAKgBACAEAIAQFDFMOir4rOFnj3XDgvPqNPXqIbJo0qtlXLMTi62lmo5THM2x4Hgey+S1egt00vq7r5OzTfG1duSDaXjwbjmvVHAD9pZusBtKNhGAumwBtAIoNkjHSALSNIwROmvkt41YLELnDhqt1FkixRue8C13HQLaqieolsrRlbdCqOZHUYfh7ImXLBtHVfWU1RpioxODZZKbyy+2BrdAAtTMkbGgJGMQFiONAWGCyAegBACAEAIAQAgBAVK6hhrIiyZlwqygpLbLglNxeUcbimD1NDd8bXTQ9PeH7rhar0fDcqP0OlTrVxZ+pltkBzByXFnU4vbJYZ74yUu6Hh6z2Fhd51TYBDKp2AY6Q2V1WgRF7itFFEgGucjaROSSKMvkEcTduQ8OXUrbT6WzVPC4+TC7URqXfk6XCcJENnyeaQ6lfU0aeGnhsgv9nEstlZLMjabFbQLYzHiK6AkbEgJWxhAStaAgHIAQAgBACAEAIAQAgBAMfG14sQgMTEvDlLVkuDNiT52GxWdlMLViayXhZKHtZz9X4ar4M4JWSjk8WPqFzbPSapextHshrpL3LJmS0mIQ3EtFJ3YQ7/AH+S8c/Sblw0z0R11b5IDIWe/FK3vGV55en6hflNVqqn+Ybvmng8/wBBUfg9R/Cy3Xr/AIkSxMmlNoaaZ/Ztv1WkPTdRJ8YM5ayqPk06PAa+ozm2YWHgDdy99PpMI97Xn7Hls17faCwdNhuCw0bfI3Pi46ldaMYxWIrCPA5NvLNQRADJWIHCNAODEA4NQC2CAVACAEAIAQAgBACAEAIAQAgBAIQCgI3QxnVqAidRQO1YEA32Cn+QICRlLE3RgQEzWNboEAtkAWQCoAQAgBACAEAIAQAgP//Z";

    request.AddParameter("client_id", this._clientID);
    request.AddParameter("client_secret", this._clientSecret);
    request.AddParameter("grant_type", "client_credentials");

    request.AddHeader("GData-Version", "3");
    request.AddHeader("Content-Type", "image/jpeg");
    request.AddHeader("accessToken", Authenticantion());
    request.AddBody(@"Content-Length: 5951
                      Slug: banana.jpeg " + imageBase64);

    var tResponse = restclient.Execute(request);
    var responseJson = tResponse.Content;
    var token = Authenticantion();

    return responseJson;
}

OAuth2 身份验证通常发生在用户流程中,用户使用用户进行登录并允许(或不允许)应用程序的某些范围(例如访问 Picasa,对 Google 驱动器只读) .

您可以通过此 link 查看完整流程:OAuthPlayground。并在流程结束时获取您自己的令牌以用于您的 HTTP 请求。需要注意的一件事是刷新令牌,在操场内可以设置为永不过期。但在现实世界中,您需要实现此刷新。

Picasa API 有点过时,我建议您改用 GoogleDrive,它更灵活,而且是最新的。 GoogleDrive_API_Docs

也可以通过 OAuth2_ Server to server 实现此流程,但我以前从未尝试过。