Bhuwan Pandey
Bhuwan Pandey

Reputation: 513

Authentication request returned unexpected result: 404

Exception Details:

Google.GData.Client.GDataRequestException: Execution of authentication request returned unexpected result: 404

Here is my code:

protected void Button1_Click(object sender, EventArgs e)
{
    DataSet ds = new DataSet();
    ds.Tables.Add("GmailContacts");
    ds.Tables[0].Columns.Add("EmailID");
            
    RequestSettings rs = new RequestSettings("Gmail", txtUserName.Value, txtPassword.Value);
    rs.AutoPaging = true;
    ContactsRequest cr = new ContactsRequest(rs);
            
    Feed<Contact> feed = cr.GetContacts();

    foreach (Contact contact in feed.Entries)
    {
        foreach (Email email in contact.Emails)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr["EmailID"] = email.Address.ToString();
            ds.Tables[0].Rows.Add(dr);
        }
    }
    GridView1.DataSource = ds;
    GridView1.DataBind();
}

Upvotes: 0

Views: 4583

Answers (1)

Adrian Botor
Adrian Botor

Reputation: 372

Check my own solution

Steps:

  1. Go to https://console.developers.google.com/project and create project.
  2. Select project, select APIs & auth from top left corner menu.
  3. Select Credentials
  4. Create OAuth with button Create new Client ID (Application type - Installed Aplication.
  5. Fill field Product Name
  6. Save

After that you got Client ID for native application with: Client ID, Client secret, Redirect URIs

Install Google.Apis.Auth from NuGet

Code

string clientId = null; // https://console.developers.google.com/project/xxx
string clientSecret = null; // https://console.developers.google.com/project/xxx
string accessCode = null; // You will get this code after GetAccessToken method
string redirectUri = null; // https://console.developers.google.com/project/xxx
string applicationName = null; // https://console.developers.google.com/project/xxx
// Scopes https://support.google.com/a/answer/162106?hl=en
string scopes = null; // put your scope like https://www.google.com/m8/feeds/
string accessType = "offline";
string tokenType = "refresh";

OAuth2Parameters parameters = new OAuth2Parameters
{
    ClientId = clientId,
    ClientSecret = clientSecret,
    RedirectUri = redirectUri,
    Scope = scopes,
    AccessType = accessType,
    TokenType = tokenType
};

if (accessCode == null)
{
    string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
    // Start webbrowser
    Process.Start(url);

    // Load code from web via popup, etc.
    parameters.AccessCode = accessCodeFromWeb;
}

// Check accessToken and refreshToken
// After first acceess with  GetAccessToken you will get that information
if (accessToken == null || refreshToken == null)
{
    OAuthUtil.GetAccessToken(parameters);

    // Save yours accessToken and refreshToken for next connection
    accessToken = parameters.AccessToken;
    refreshToken = parameters.RefreshToken;
}
else
{   
    // Restore your token from config file, etc.
    parameters.AccessToken = accessToken;
    parameters.RefreshToken = refreshToken;
}

RequestSettings rs = new RequestSettings(applicationName, parameters);

return new ContactsRequest(rs);

Upvotes: 1

Related Questions