Vladimir Vlasov
Vladimir Vlasov

Reputation: 2090

Uploading a profile photo for g+ account via C#

I want upload a profile photo for Google Plus account by C# with HttpWebRequest. How to find out method in the page source for this post query? I'm using Web Inspector (Chrome Developers Tools), but cannot find code which executing after clicking by "Set the profile photo" button.

Here code for uploading photo, which I found in the Internet:

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;
    }
}

But this code generate exception "Method not Allowed", when executing second query.

Also, I already have successfull code for login and post text.

Upvotes: 0

Views: 1044

Answers (1)

Chirag Shah
Chirag Shah

Reputation: 3674

It is not possible to update a user's profile photo with the official Google+ API.

For a list of supported APIs in the Google+ API, see: https://developers.google.com/+/api/latest

The endpoint https://plus.google.com/_/ is not supported API by Google, and can change at anytime.

Upvotes: 1

Related Questions