Thứ Hai, 22 tháng 4, 2019

Đăng bài lên blogspot bằng google gdata api

Đăng Bởi: Admin - tháng 4 22, 2019 - 0 Bình Luận




https://c-sharp-asp-snippets.blogspot.com/2015/03/posting-to-blogspot-using-google-gdata.html?view=flipcard


https://c-sharp-asp-snippets.blogspot.com/?view=flipcard


hướng dẫn google gdata api
Trong hướng dẫn này, tôi sẽ chỉ cho bạn cách sử dụng  Google GData API để đăng lên Blogspot /  Blog Blogger từ google bằng  Ứng dụng C # hoặc .Net . Bạn cũng sẽ thấy cách  tải hình ảnh lên google bằng API  và đính kèm nó vào bài đăng trên blog và cách thêm thẻ trực tiếp từ Ứng dụng C # của bạn.
Trước hết, bạn phải tải xuống Google_Data_API tại đây  hoặc tải trực tiếp các lib  mà tôi đã sử dụng trong hướng dẫn này  tại đây .
Ok, sau khi bạn đã tải xuống  Google API Libs,  hãy bắt đầu.

1. CÁC KHOẢN PHỤ TÙNG GDATA LIBS

Chẳng hạn libs trên  C: \ Libs \  chẳng hạn.
Tạo dự án C # mới >> Chuyển đến  Solution Explorer  >> Nhấp chuột phải vào  Dự án  >>  Thêm  >>  Tham khảo
Google api libs gdata
Bây giờ, hãy đi tới Duyệt >> Nhấp vào Duyệt nút >> Chuyển đến  C: \ Libs \ Google API \ Samples  và chọn: 
"Google.GData.Photos.dll" "ExecRequest" "Google.GData.Blogger.dll" "Google.GData.Client.DLL" "Google.GData.Extensions.dll" >> Thêm
blogger phụ thuộc google api
Bây giờ bạn phải thêm vào mã của bạn:
1
2
3
4
using Google.GData.Blogger;
using Google.GData.Client;
using Google.GData.Photos;
using Google.GData.Extensions;
Và đó là tất cả với libs, bây giờ hãy đăng một cái gì đó

2. CÁCH ĐĂNG KÝ ĐẾN BLOGSPOT

Một loại bài đăng đơn giản trông giống như:
1
2
3
4
5
6
7
số 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.Scheme = new Uri("http://www.blogger.com/atom/ns#");
    cat.Term = tag;
    post.Categories.Add(cat);
}
post.IsDraft = false;
Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + "yourBlogspotID" + "/posts/default");
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...");
Và bạn có thể tải lên một hình ảnh lên google theo cách này:
1
2
3
4
5
6
7
số 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;
}
Chức năng  tải hình ảnh lên google  sẽ trả về liên kết trực tiếp đến hình ảnh của bạn mà bạn sẽ sử dụng trong bài đăng của mình như:
<img src="returnedString" />
blogId : Bạn có thể lấy nó từ Blogger. Đăng nhập vào blogger >> Truy cập Blog mong muốn và liên kết giống như: Tôi nghĩ bạn hiểu BlogID ở đâu
https://www.blogger.com/blogger.g?blogID=5004180000000060001

3.OR GAY TRỰC TIẾP MỘT CHỨC NĂNG ĐĂNG KÝ ĐẾN BLOGSPOT, HOẶC HAI

1
2
3
4
5
6
7
số 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.Scheme = new Uri("http://www.blogger.com/atom/ns#");
            cat.Term = label2;
            post.Categories.Add(cat);
            this.maskedTextBox5.Text = this.maskedTextBox5.Text + label2 + ",";
            break;
        }
        else
        {
            AtomCategory cat = new AtomCategory();
            cat.Scheme = new Uri("http://www.blogger.com/atom/ns#");
            cat.Term = label;
            post.Categories.Add(cat);
        }
    }
    post.IsDraft = false;
    ///////////////publishing post/_/_/_/_/_/_\_|_/_)_(_--> () - {} - [] 0_o
    Uri blogFeedUri = new Uri("http://www.blogger.com/feeds/" + blogId + "/posts/default");
    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;
}
Nếu bạn có hình ảnh trong bài đăng của mình, bạn phải đảm bảo rằng bài đăng đó sẽ giống như: 
<img src="[IMGLINK]" /> vwhere [IMGLINK] sẽ được thay thế bằng liên kết hình ảnh mà bạn đã chọn đường dẫn.

3.A LITTLE C # ỨNG DỤNG MÀ SAU BÀI ĐĂNG CHO BLOGSPOT:

google api đăng lên blogspot ứng dụng mạng
Bạn có thể tải C # Post lên Blogspot bằng dự án Google API  tại đây.
Bạn phải có libs  C:\Libs\ và thư mục này với libs C:\Libs\Google API\Samples

5. KẾT LUẬN

Bạn có thể tìm hiểu thêm về Google API trên Diễn đàn nhà phát triển Google GData như tạo album và những thứ khác nhưng tôi nghĩ những ví dụ này là một điểm tốt để bắt đầu.
Hãy bình luận nếu làm việc cho bạn hay không và tại sao không nếu có :))

Phản Hồi Độc Giả

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é