Posting to blogspot using google gdata api
https://www.codeproject.com/Articles/421895/Blog-integration-into-ASP-NET-website-using-GData
Incede.blogger.Project
Google.GData.Blogger.dll
Google.GData.Client
Incede.blogger.TestProject
1.ADDING GDATA LIBS DEPENDENCIES
UnRar libs on C:\Libs\ for example.
Create a New C# Project >> Go to Solution Explorer >> Right-Click on Project >> Add >> Reference…
Now go to Browse >> Click Browse Button >> Go to C:\Libs\Google API\Samples and select:
"Google.GData.Photos.dll" "ExecRequest" "Google.GData.Blogger.dll" "Google.GData.Client.DLL" "Google.GData.Extensions.dll" >> Add
Now you have to add to your code:
1
2
3
4
using Google.GData.Blogger;
using Google.GData.Client;
using Google.GData.Photos;
using Google.GData.Extensions;
And that’s all with libs, now let’s post something…
"Google.GData.Photos.dll" "ExecRequest" "Google.GData.Blogger.dll" "Google.GData.Client.DLL" "Google.GData.Extensions.dll" >> Add
1
2
3
4
| using Google.GData.Blogger;using Google.GData.Client;using Google.GData.Photos;using Google.GData.Extensions; |
2.HOW TO POST TO BLOGSPOT
A simple kind of post looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
CheckForIllegalCrossThreadCalls = false;
Service service = new Service("blogger", "googleAPIshittyName");
service.Credentials = new GDataCredentials("yourGoogleUsername@gmail.com", "yourGooglePassword");
GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
factory.AccountType = "GOOGLE";
AtomEntry post = new AtomEntry();
post.Title.Text = "YourPostTitle";
post.Content = new AtomContent();
post.Content.Content = "Post Body, Descrption...";
post.Content.Type = "xhtml";
string tags = "tag1,tag2,tag3";
string[] tagsS = tags.Split(',');
foreach(string tag in tagsS)
{
AtomCategory cat = new AtomCategory();
cat.Term = tag;
post.Categories.Add(cat);
}
post.IsDraft = false;
AtomEntry createdEntry = null;
try
{
createdEntry = service.Insert(blogFeedUri, post);
MessageBox.Show("Done!");
}
catch (GDataRequestException exception)
{
if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts");
}
if (createdEntry == null)
MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags...");
And you can upload an image to google in this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public string uploadImageToGoogle(string path, string username, string password, string blogId)
{
if (!System.IO.File.Exists(path))
return "Error! Image file not found";
///////////////////////token session and shits...///////////
PicasaService service = new PicasaService("HowToFix.PRO");
service.setUserCredentials(username, password);
/////////////////cdefault album is dropBox or something////
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username));
System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
System.IO.FileStream fileStream = fileInfo.OpenRead();
PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile");
fileStream.Close();
PhotoAccessor ac = new PhotoAccessor(entry);
string contentUrl = entry.Media.Content.Attributes["url"] as string;
return contentUrl;
}
Th function to upload image to google will return direct link to your image which you will use in your post like:
<img src="returnedString" />
*blogId: You can take it from Blogger. Login to blogger >> Go to Desired Blog and link looks like:
https://www.blogger.com/blogger.g?blogID=5004180000000060001
I think you understand where the BlogID is…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| CheckForIllegalCrossThreadCalls = false;Service service = new Service("blogger", "googleAPIshittyName");service.Credentials = new GDataCredentials("yourGoogleUsername@gmail.com", "yourGooglePassword");GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;factory.AccountType = "GOOGLE";AtomEntry post = new AtomEntry();post.Title.Text = "YourPostTitle";post.Content = new AtomContent();post.Content.Content = "Post Body, Descrption...";post.Content.Type = "xhtml";string tags = "tag1,tag2,tag3";string[] tagsS = tags.Split(',');foreach(string tag in tagsS){ AtomCategory cat = new AtomCategory(); cat.Term = tag; post.Categories.Add(cat);}post.IsDraft = false;AtomEntry createdEntry = null;try{ createdEntry = service.Insert(blogFeedUri, post); MessageBox.Show("Done!");}catch (GDataRequestException exception){ if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts") MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts");}if (createdEntry == null) MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags..."); |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| public string uploadImageToGoogle(string path, string username, string password, string blogId){ if (!System.IO.File.Exists(path)) return "Error! Image file not found"; ///////////////////////token session and shits.../////////// PicasaService service = new PicasaService("HowToFix.PRO"); service.setUserCredentials(username, password); /////////////////cdefault album is dropBox or something//// Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username)); System.IO.FileInfo fileInfo = new System.IO.FileInfo(path); System.IO.FileStream fileStream = fileInfo.OpenRead(); PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile"); fileStream.Close(); PhotoAccessor ac = new PhotoAccessor(entry); string contentUrl = entry.Media.Content.Attributes["url"] as string; return contentUrl;} |
<img src="returnedString" />https://www.blogger.com/blogger.g?blogID=5004180000000060001I think you understand where the BlogID is…
3.OR…DIRECTLY A FUNCTION TO POST TO BLOGSPOT, OR TWO…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
public void sendPostToBlogspot(string blogId, string username, string password, string title, string postBody, string tags)
{
CheckForIllegalCrossThreadCalls = false;
Service service = new Service("blogger", "googleAPIshittyName");
service.Credentials = new GDataCredentials(username, password);
GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory;
factory.AccountType = "GOOGLE";
/////////////////////////////////////////////////////////////
if (postBody.Contains("[IMGLINK]"))
{
postBody = postBody.Replace("[IMGLINK]", uploadImageToGoogle("image.png", username, password, blogId));
}
///////post////////////////////////////////
AtomEntry post = new AtomEntry();
post.Title.Text = title;
post.Content = new AtomContent();
post.Content.Content = postBody;
post.Content.Type = "xhtml";
int length = 0;
//make sure that tags are not too long
string[] tagsS = tags.Split(',');
foreach (string label in tagsS)
{
length = length + label.Count();
if (length > 198)
{
string label2 = label.Substring(0, 199);
AtomCategory cat = new AtomCategory();
cat.Term = label2;
post.Categories.Add(cat);
this.maskedTextBox5.Text = this.maskedTextBox5.Text + label2 + ",";
break;
}
else
{
AtomCategory cat = new AtomCategory();
cat.Term = label;
post.Categories.Add(cat);
}
}
post.IsDraft = false;
///////////////publishing post/_/_/_/_/_/_\_|_/_)_(_--> () - {} - [] 0_o
AtomEntry createdEntry = null;
try
{
createdEntry = service.Insert(blogFeedUri, post);
MessageBox.Show("Done!");
}
catch (GDataRequestException exception)
{
if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts")
MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts");
}
if (createdEntry == null)
MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags...");
}
public string uploadImageToGoogle(string path, string username, string password, string blogId)
{
if (!System.IO.File.Exists(path))
return "Error! Image file not found";
///////////////////////token session and shits...///////////
PicasaService service = new PicasaService("HowToFixPRO");
service.setUserCredentials(username, password);
/////////////////cdefault album is dropBox or something////
Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username));
System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
System.IO.FileStream fileStream = fileInfo.OpenRead();
PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile");
fileStream.Close();
PhotoAccessor ac = new PhotoAccessor(entry);
string contentUrl = entry.Media.Content.Attributes["url"] as string;
return contentUrl;
}
If you have images in your post you have to make sure that post will look like:
<img src="[IMGLINK]" /> vwhere [IMGLINK] will e replaced with image link which you have selected path for.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| public void sendPostToBlogspot(string blogId, string username, string password, string title, string postBody, string tags){ CheckForIllegalCrossThreadCalls = false; Service service = new Service("blogger", "googleAPIshittyName"); service.Credentials = new GDataCredentials(username, password); GDataGAuthRequestFactory factory = (GDataGAuthRequestFactory)service.RequestFactory; factory.AccountType = "GOOGLE"; ///////////////////////////////////////////////////////////// if (postBody.Contains("[IMGLINK]")) { postBody = postBody.Replace("[IMGLINK]", uploadImageToGoogle("image.png", username, password, blogId)); } ///////post//////////////////////////////// AtomEntry post = new AtomEntry(); post.Title.Text = title; post.Content = new AtomContent(); post.Content.Content = postBody; post.Content.Type = "xhtml"; int length = 0; //make sure that tags are not too long string[] tagsS = tags.Split(','); foreach (string label in tagsS) { length = length + label.Count(); if (length > 198) { string label2 = label.Substring(0, 199); AtomCategory cat = new AtomCategory(); cat.Term = label2; post.Categories.Add(cat); this.maskedTextBox5.Text = this.maskedTextBox5.Text + label2 + ","; break; } else { AtomCategory cat = new AtomCategory(); cat.Term = label; post.Categories.Add(cat); } } post.IsDraft = false; ///////////////publishing post/_/_/_/_/_/_\_|_/_)_(_--> () - {} - [] 0_o AtomEntry createdEntry = null; try { createdEntry = service.Insert(blogFeedUri, post); MessageBox.Show("Done!"); } catch (GDataRequestException exception) { if (exception.ResponseString == "Blog has exceeded rate limit or otherwise requires word verification for new posts") MessageBox.Show("Blog has exceeded rate limit or otherwise requires word verification for new posts"); } if (createdEntry == null) MessageBox.Show("Something went wrong, atricle was not published...\nCheck all html tags..."); }public string uploadImageToGoogle(string path, string username, string password, string blogId){ if (!System.IO.File.Exists(path)) return "Error! Image file not found"; ///////////////////////token session and shits.../////////// PicasaService service = new PicasaService("HowToFixPRO"); service.setUserCredentials(username, password); /////////////////cdefault album is dropBox or something//// Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(username)); System.IO.FileInfo fileInfo = new System.IO.FileInfo(path); System.IO.FileStream fileStream = fileInfo.OpenRead(); PicasaEntry entry = (PicasaEntry)service.Insert(postUri, fileStream, "image/png", "nameOfYourFile"); fileStream.Close(); PhotoAccessor ac = new PhotoAccessor(entry); string contentUrl = entry.Media.Content.Attributes["url"] as string; return contentUrl;} |
<img src="[IMGLINK]" /> vwhere [IMGLINK] will e replaced with image link which you have selected path for.3.A LITTLE C# APPLICATION WHICH WILL POST TO BLOGSPOT:
You can download C# Post to Blogspot using Google API project here.
You had to have libs on C:\Libs\ and this folder with libs C:\Libs\Google API\Samples
C:\Libs\ and this folder with libs C:\Libs\Google API\Samples


Một số lưu ý khi bình luận
Mọi bình luận sai nội quy sẽ bị xóa mà không cần báo trước (xem nội quy)
Bấm Thông báo cho tôi bên dưới khung bình luận để nhận thông báo khi admin trả lời
Để bình luận một đoạn code, hãy mã hóa code trước nhé