0

我想通过 C# 使用 HttpWebRequest 为 Google Plus 帐户上传个人资料照片。如何在此帖子查询的页面源中查找方法?我正在使用 Web Inspector(Chrome 开发人员工具),但无法找到单击“设置个人资料照片”按钮后执行的代码。

这里上传照片的代码,我在网上找到的:

public bool UploadPhoto( Photo photo, string accountLine, string googleCookie )
{
    try
    {
        string query, upload_id;

        // проверка ошибок
        if( photo.Size == 0 ) return false;
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create( "https://plus.google.com/_/upload/photos/resumable?authuser=0" );
        string content = "{\"protocolVersion\":\"0.8\",\"createSessionRequest\":{\"fields\":[{\"external\":{\"name\":\"file\",\"filename\":\"image-8453.jpg\",\"put\":{},\"size\":" +
                                    photo.Size.ToString() + "}},{\"inlined\":{\"name\":\"async_thumbnail\",\"content\":\"false\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"auto_create_album\",\"content\":\"profile_photos.draft\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"disable_asbe_notification\",\"content\":\"true\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"enable_face_detection\",\"content\":\"true\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"use_upload_size_pref\",\"content\":\"true\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"title\",\"content\":\"image-8453.jpg\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"addtime\",\"content\":\"1341478011235\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"batchid\",\"content\":\"1341477814870\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"album_name\",\"content\":\"5 \\u043b\\u0438\\u043f\\u043d\\u044f 2012 \\u0440.\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"album_abs_position\",\"content\":\"0\",\"contentType\":\"text/plain\"}},{\"inlined\":{\"name\":\"client\",\"content\":\"es-profile-signup\",\"contentType\":\"text/plain\"}}]}}";
        byte[] bcontent;

        string header = "Cookie: " + googleCookie;

        bcontent = Encoding.Default.GetBytes( content );

        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
        req.ContentLength = bcontent.Length;
        req.UserAgent = userAgent;
        req.Referer = "https://plus.google.com/u/0/_/notifications/frame?sourceid=1&hl=ru&origin=https%3A%2F%2Fwww.google.com&uc=1&jsh=m%3B%2F_%2Fabc-static%2F_%2Fjs%2Fgapi%2F__features__%2Frt%3Dj%2Fver%3DVO1wk6ea0_A.en.%2Fsv%3D1%2Fam%3D!OgKRzknZ1ASBPEY3DA%2Fd%3D1";
        req.Headers.Add( header );

        req.GetRequestStream().Write( bcontent, 0, bcontent.Length );
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        StreamReader sr = new StreamReader( resp.GetResponseStream() );
        content = sr.ReadToEnd();

        Match m = Regex.Match( content, "\"url\":\"(?<url>[^\"]+)\"" );
        if( !m.Success ) return false;
        query = m.Groups[ "url" ].Value;
        m = Regex.Match( content, "upload_id\":\"(?<upload_id>[^\"]+)\"" );
        if( !m.Success ) return false;
        upload_id = m.Groups[ "upload_id" ].Value;

        req = (HttpWebRequest)WebRequest.Create( query );
        req.ContentLength = photo.Size;
        req.Method = "POST";
        req.ContentType = "application/octet-stream";
        req.Headers.Add( "Accept-Charset", "utf-8" );
        req.Headers.Add( "Origin", "plus.google.com" );
        req.Headers.Add( "X-GUploader-No-308", "yes" );
        req.Headers.Add( "X-HTTP-Method-Override", "PUT" );
        req.Headers.Add( header );
        req.Accept = "*/*";
        req.UserAgent = userAgent;
        req.GetRequestStream().Write( photo.Data, 0, photo.Size );
        resp = (HttpWebResponse)req.GetResponse();

        return true;
    }
    catch( Exception ex )
    {
        Log( accountLine + ": Error with uploading photo: " + ex.Message );
        return false;
    }
}

但是此代码在执行第二个查询时会生成异常“方法不允许”。

另外,我已经有登录和发布文本的成功代码。

4

1 回答 1

1

无法使用官方 Google+ API 更新用户的个人资料照片。

有关 Google+ API 中支持的 API 列表,请参阅: https ://developers.google.com/+/api/latest

Google 不支持该端点https://plus.google.com/_/的 API,并且可以随时更改。

于 2012-09-26T22:44:37.463 回答