Google+时刻发布.NET API 401未经授权的错误

时间:2014-09-03 12:44:51

标签: c# android .net rest google-plus

我们在Google+ REST API服务中的插入时刻方法存在问题。

我们的用例如下:我们的Android应用程序从GoogleAuthUtil获取以下类的离线授权码:

public class GetGooglePlusToken extends AsyncTask<String, Void, String> {

    private Context context;
    private GoogleApiClient apiClient;

    public GetGooglePlusToken(Context context, GoogleApiClient apiClient)
    {
        this.context = context;
        this.apiClient = apiClient;
    }

    @Override
    protected String doInBackground(String... params) {
        String serverClientId = "xxxxxxx.apps.googleusercontent.com" // Client ID for web application from google developer console
        String scopesString = Scopes.PLUS_LOGIN + " " + Scopes.PLUS_ME;
        String scope = "oauth2:server:client_id:" + serverClientId + ":api_scope:" + scopesString;
        Bundle appActivities = new Bundle();
        appActivities.putString(GoogleAuthUtil.KEY_REQUEST_VISIBLE_ACTIVITIES, "http://schemas.google.com/AddActivity");
        String code = GoogleAuthUtil.getToken(
                        context,                                     
                        Plus.AccountApi.getAccountName(apiClient),  
                        scope,                                       
                        appActivities                                 
                    );

        return code;
    }
}

我们将授权代码发送到asp.net webapi服务器。授权代码将由服务器替换为访问令牌。使用此访问令牌,我们尝试使用以下代码将某些内容发布到用户的墙上,但我们总是会收到401未经授权的http错误:

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
    ClientSecrets = new ClientSecrets()
    {
        ClientId = "xxxxxxx.apps.googleusercontent.com" // ClientID for web application from google developer console
        ClientSecret = "yyyyyyyyyyyyy" // Client Secret from google developer console
    },
    Scopes = new string[] { PlusService.Scope.PlusLogin, PlusService.Scope.PlusMe },
});

var tokenResponse = flow.ExchangeCodeForTokenAsync("", authorizationCode, "", CancellationToken.None).Result;

var credential = new UserCredential(flow, "me", tokenResponse);

var body = new Moment();
var itemScope = new ItemScope();

itemScope.Id = "customid01";
itemScope.Type = "";
itemScope.Description = "The description for the action";
itemScope.Name = "An example of add activity";

body.Object = itemScope;
body.Type = " http://schemas.google.com/AddActivity";

var plusService1 = new PlusService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApiKey = ConfigurationManager.AppSettings["GoogleKeyForBrowserApps"], // Key for browser applications from developer console
});

// ok
var me1 = plusService1.People.Get("me").Execute();

var insert1 = new MomentsResource.InsertRequest(plusService1, body, "me", MomentsResource.InsertRequest.CollectionEnum.Vault);
// 401 error
Moment wrote1 = insert1.Execute();

在Google+开发者网站上,插入时刻开发者控制台会注意到当前开发者控制台无法发布:https://developers.google.com/+/api/latest/moments/insert。请参见页面底部:重要!下面的API Explorer当前不起作用。它返回“401 Unauthorized”错误。它仅对开发人员控制台有效,但我们会收到相同的错误。

您知道我们如何解决这个问题?谢谢你的帮助!

0 个答案:

没有答案