验证请求返回意外结果:404

时间:2015-06-10 18:44:00

标签: c# asp.net .net api

异常详细信息:Google.GData.Client.GDataRequestException:执行身份验证请求返回意外结果:404

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

检查我自己的解决方案

步骤:

  1. 转到https://console.developers.google.com/project并创建项目。
  2. 选择项目,选择API&amp;从左上角菜单中验证。
  3. 选择凭据
  4. 使用按钮创建OAuth创建新的客户端ID(应用程序类型 - 已安装的应用程序。
  5. 填写字段产品名称
  6. 保存
  7. 之后,您获得了本机应用程序的客户端ID:  客户ID,  客户秘密,  重定向URI

    从NuGet安装Google.Apis.Auth

    代码

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