在Google+墙上分享文字。 [英] Sharing text on Google+ wall.

查看:65
本文介绍了在Google+墙上分享文字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我想在Google+墙上分享文字(和图片)。尝试完成此操作时出错:

Hi, all! I want to sharing the text (and images) on Google+ wall. Error occurs when try to this done:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured"
   }
  ],
  "code": 403,
  "message": "Access Not Configured"
 }
}





尝试了不同的选项,但无法删除错误。请帮助我!



以下是源代码:



Tried different options, but could not remove the error. Please, help me!

Below is the source code:

public partial class GooglePlusLoginPage : PhoneApplicationPage
    {
        App thisApp = Application.Current as App;

        string _clientId = "<client_id>";
        string _redirctedUri = "http://localhost";

        public class AccessToken
        {
            public string access_token { get; set; }
            public string token_type { get; set; }
            public int expires_in { get; set; }
            public string id_token { get; set; }
            public string refresh_token { get; set; }
        }

        AccessToken _accessToken;

        public GooglePlusLoginPage()
        {
            InitializeComponent();
            
            this.Loaded += new RoutedEventHandler(GooglePlus_LoginPage_Loaded); 
        }

        //load google plus login page 
        void GooglePlus_LoginPage_Loaded(object sender, RoutedEventArgs e)
        {
            var url = "https://accounts.google.com/o/oauth2/auth?" +
               "response_type=code&" +
               "redirect_uri=" + _redirctedUri +
               "&scope=" +
                      "https://www.googleapis.com/auth/plus.stream.write%20" +
                      "https://www.googleapis.com/auth/plus.me&" +
               "client_id=" + _clientId;

            webBrowserGooglePlusLogin.IsScriptEnabled = true;
            webBrowserGooglePlusLogin.Navigate(
                new Uri(url, UriKind.RelativeOrAbsolute));
        }

        private void webBrowserGooglePlusLogin_Navigated(
           object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            webBrowserGooglePlusLogin.Visibility = Visibility.Visible;
        }

        private async void webBrowserGooglePlusLogin_Navigating(
                                       object sender, NavigatingEventArgs e)
        {
            if (e.Uri.Host.Equals("localhost"))
            {
                webBrowserGooglePlusLogin.Visibility = Visibility.Collapsed;

                e.Cancel = true;
                int pos = e.Uri.Query.IndexOf("=");

                 string messageCode = 
                     pos > -1 ? e.Uri.Query.Substring(pos + 1) : null;

                if (messageCode != null)
                {
                    string parametrs = 
                        "code=" + messageCode + 
                        "&client_id=" + _clientId + 
                        "&redirect_uri=" + _redirctedUri + 
                        "&grant_type=authorization_code";

                    HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(
                                    "https://accounts.google.com/o/oauth2/token");
                    hwr.Method = "POST";
                    hwr.ContentType = "application/x-www-form-urlencoded";

                    await AddBody(hwr, parametrs);

                    string result = await GetResponseResult(hwr);

                    _accessToken = await 
                      JsonConvert.DeserializeObjectAsync<AccessToken>(result);

                    SharedData();                   
                }
            }
        }



我认为这种方法有问题:


I think this method is something wrong:

async void SharedData()
{
    string url =
        "https://www.googleapis.com/plusDomains/v1/people/me/activities";

    Uri uri = new Uri(url);

    string sharedData =
    "{" +
          "\"object\": {" +
            "\"originalContent\": \"Hello, World!!!\"" +
          "}," +
          "\"access\": {" +
            "\"items\": [{" +
                "\"type\": \"person\"," +
                "\"id\": \"me\"" +
            "}]," +
            "\"domainRestricted\": true" +
          "}" +
    "}";

    HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(uri);
    hwr.Headers[HttpRequestHeader.Authorization] =
                              "Bearer " + _accessToken.access_token;
    hwr.ContentType = "application/json";
    hwr.Method = "POST";

    await AddBody(hwr, sharedData);

    //Inside of method  "response.GetResponseStream()"
    // an exception is thrown  !!!
    string result = await GetResponseResult(hwr);
}



助手方法:


Helpers methods:

async Task AddBody(HttpWebRequest hwr, string body)
{
    Stream postStream = await hwr.GetRequestStreamAsync();

    byte[] byteArray = Encoding.UTF8.GetBytes(body);

    postStream.Write(byteArray, 0, byteArray.Length);
    postStream.Close();
}




async Task<string> GetResponseResult(HttpWebRequest hwr)
{
    WebException webExeption = null;

    try
    {
        WebResponse response = await hwr.GetResponseAsync();

        return await GetResponseResult(response);
    }
    catch (WebException we)
    {
        webExeption = new WebException(
            we.Message, we.InnerException, we.Status, we.Response);
    }

    if (webExeption != null)
    {
        string result = await GetResponseResult(webExeption.Response);

        MessageBox.Show(result, webExeption.Message, MessageBoxButton.OK);
    }

    return String.Empty;
}




    async Task<string> GetResponseResult(WebResponse response)
    {
        Stream stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);

        string result = await reader.ReadToEndAsync();

        reader.Close();
        stream.Close();

        return result;
    }
}

推荐答案

您是否尝试过此链接http://stackoverflow.com/questions/12956251/make-google-plus-post-through-net-c [ ^ ]



也看到这个

http://forums.asp.net/t/1899903.aspx [ ^ ]



尝试告诉我们这是否对您不起作用。



谢谢

Ganesh
Have you tried this link http://stackoverflow.com/questions/12956251/make-google-plus-post-through-net-c[^]

and also see this
http://forums.asp.net/t/1899903.aspx[^]

try and let us know if this does not work for you.

Thanks
Ganesh


这篇关于在Google+墙上分享文字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆