Innocent Criminal
Innocent Criminal

Reputation: 306

How to fetch classrooms from google classroom using API calls?

So I am trying to fetch my classes from my google classroom into my application: Here is my google url:

var Googleurl = "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=" + googleplus_redirect_url + "&prompt=consent&response_type=code&client_id=" + googleplus_client_id + "&scope=https://www.googleapis.com/auth/userinfo.profile+https://www.google.com/m8/feeds/+https://www.googleapis.com/auth/drive+https://www.googleapis.com/auth/drive.appdata+https://www.googleapis.com/auth/drive.file+https://www.googleapis.com/auth/drive.metadata+https://www.googleapis.com/auth/classroom.courses+https://www.googleapis.com/auth/classroom.profile.emails+https://www.googleapis.com/auth/classroom.profile.photos+https://www.googleapis.com/auth/classroom.rosters+https://www.googleapis.com/auth/classroom.rosters.readonly&access_type=offline";

After this I request for the access codes through:

 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
                    webRequest.Method = "POST";
                    Parameters = "code=" + code + "&client_id=" + googleplus_client_id + "&client_secret=" + googleplus_client_sceret + "&redirect_uri=" + googleplus_redirect_url + "&grant_type=authorization_code";
                    byte[] byteArray = Encoding.UTF8.GetBytes(Parameters);
                    webRequest.ContentType = "application/x-www-form-urlencoded";
                    webRequest.ContentLength = byteArray.Length;
                    Stream postStream = webRequest.GetRequestStream();
                    // Add the post data to the web request
                    postStream.Write(byteArray, 0, byteArray.Length);
                    postStream.Close();

                    WebResponse response = webRequest.GetResponse();
                    postStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(postStream);
                    string responseFromServer = reader.ReadToEnd();

                    GooglePlusAccessToken serStatus = JsonConvert.DeserializeObject<GooglePlusAccessToken>(responseFromServer);

I am able to get the access code as well as the refresh token all fine.

Now when I wish to fetch my classroom by:

 string p="https://classroom.googleapis.com/v1/courses";
 string auth = "Bearer "+access_token;
 private bool GetClasses(string p,string auth)
    {
        using (var client = new WebClient())
        {
            var uri = new Uri(p);
            //client.DefaultRequestHeaders.Authorization=new AuthenticationHeaderValue("Bearer", auth);
            string req="Authorization: "+ auth;
            client.Headers.Add(req);

            var response = client.DownloadString(uri);

        }

        return true;
    }

This code return an error of type:System.Net.WebException: {"The remote server returned an error: (403) Forbidden."}

I have used the same access_token to get all other scopes as shown in the scopes parameter in mu Googleurl. However I am unable to access my classes even though I've added the respective scopes for it.

Upvotes: 0

Views: 519

Answers (1)

Innocent Criminal
Innocent Criminal

Reputation: 306

Apparently to use google classroom related functionalities we need to enable the google classroom Api. As silly as it may sound I was ignorant about it(As I had already enabled the google Api service). Just had to activate the classroom api and the code worked like a charm.

Upvotes: 1

Related Questions