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