Click here to Skip to main content
15,888,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
    }
}
Posted
Updated 1-Dec-13 7:41am
v2
Comments
What is the Exception?
Mohan Gopi 4-Sep-14 5:31am    
Hi sergeykharkov,
Have you got the answer.... if you got please share that answer.... the same error i am getting........

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900